I was solving a problem before including it in my code and using List to get strings then work around with them. I got the error: "type List does not take parameters List words = new ArrayList();" after compilation. I searched but the syntax i use is correct, please whats wrong with the code?
import java.util.Scanner;
import java.util.ArrayList;
public class List{
public static void main(String args[]){
List<String> words = new ArrayList<String>();
Scanner input = new Scanner(System.in);
for (int i=0; i < 3; i++)
words.add(input.nextLine());
System.out.println(words);
}
}
This is one of the reasons you should use unique names for your own classes. You are really meaning to use java.util.List, but since you called your class List as well, the real problem is masked. Rename your class and add the import for java.util.List to fix the issue:
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class MyClass{
public static void main(String args[]){
List<String> words = new ArrayList<String>();
Scanner input = new Scanner(System.in);
for (int i=0; i < 3; i++)
words.add(input.nextLine());
System.out.println(words);
}
}
Try this:
import java.util.Scanner;
import java.util.ArrayList;
public class List{
public static void main(String args[]){
java.util.List<String> words = new ArrayList<String>();
Scanner input = new Scanner(System.in);
for (int i=0; i < 3; i++)
words.add(input.nextLine());
System.out.println(words);
}
}
Your class is called List. So by default, it is assumed that the List you declared refers to your class.
It would be better to avoid names like List for your classes.
Related
I am working on the first part of a String permutation problem and I am just looping over the first char of a string and swap it with every following char of that same String. I initialized an empty ArrayList to store all of those permutations called listeFinale. When I am printing that ArrayList, I am getting a collection of object and not values ([[C#61bbe9ba, [C#61bbe9ba, [C#61bbe9ba, [C#61bbe9ba]), how can I print each char stored in the ArrayList?
import java.util.ArrayList;
import java.util.List;
public class checkPermu {
public static void main(String[] args) {
String myString = "aabc";
applyPermu(myString);
}
public static void applyPermu(String toCheck){
char[] newString = toCheck.toCharArray();
List listeFinale = new ArrayList();
for(int i = 0 ; i < newString.length ; i ++){
char temp = newString[0];
newString[0] = newString[i];
newString[i] = temp;
listeFinale.add(newString);
System.out.println(listeFinale);
}
}
}
First of all, don't use raw types for your List please.. Change:
List listeFinale = new ArrayList();
to:
List<char[]> listeFinale = new ArrayList<>();
As for your actual problem. Those values you see are the default toString() outputs of your inner character-arrays. You could iterate over your list, and call the java.util.Arrays.toString(char[]) method for them like this:
listeFinale.forEach(arr -> System.out.println(Arrays.toString(arr)));
Or, if you want to print them back as String again, use new String(char[]):
listeFinale.forEach(arr -> System.out.println(new String(arr)));
Try it online.
The idea is that I want to enter numbers from the console and put them directly into a List. That's my way but it does not work. I have no idea how to fix it. I tried manually importing java.util...., but it still does not work.
And that is the code
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer> numbers = Arrays.stream(in.nextLine()
.split(" "))
.mapToInt(Integer::parseInt)
.collect(Collectors.toList());
}
}
Just change the last line as it follows:
List<Integer> list = Arrays.stream(in.nextLine()
.split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
I have a simple text file with the following content:
4 5 2 7
I would like Java to read this file and create an array with it. However, I want my method that I use to keep its "double" properties. I'm having a hard time getting my array command to figure it out though:
import java.util.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.io.*;
public class ReadFile {
public static void main(String[] args){
gns(Arr);
}
public static double gns(String TxtFile) throws IOException {
Path path = Paths.get("C:\\Users\\me\\files\\inputfiles");
int numLines = (int)Files.lines(path).count();
Scanner fileScanner = new Scanner(new FileReader("TxtFile.txt"));
double Arr = new ArrayList<String>();
return Arr;
}
}
It keeps giving me an array due to the type of the array.
This would do the trick :
double[] arr = Files.lines(Paths.get(PATH))
.flatMap(line -> Arrays.stream(line.split(" ")))
.mapToDouble(Double::parseDouble)
.toArray();
Here we read the file line by line and then split it using " ", parse each number and covert it to an array of double. You can then return arr[] from your method gns(String TxtFile).
Scanner#nextDouble
Give a try to the following one, full example below:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class NextDouble {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("input.txt"));
List<Double> doubles = new ArrayList<Double>();
while (sc.hasNextDouble()) {
doubles.add(sc.nextDouble());
}
System.out.println(doubles); // => [1.0, 3.0, 8.0, 6.0, 5.0]
}
}
input.txt contains the following line:
1 3 8 6 5
You can find more about scanner in the doc
You're trying to set Arr which is of type double to an ArrayList of type String, this is not possible since the types double and ArrayList are different.
If you wanted an arraylist of doubles use
ArrayList<Double> d = new ArrayList<>();
This is the original question:
Write a program that reads a set of doubles from a file, stores them in an array or ArrayList, and then prints them back out to the console (using System.out.println statements) in REVERSE order.
For example, if the input file input.txt file contains
27.3
45.6
98.3
10.1
The console output will display
10.1
98.3
45.6
27.3
And this is the code I have so far:
package reverse;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class reversed {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner numFile = new Scanner(new File("input.txt"));
ArrayList<Double> list = new ArrayList<Double>();
while (numFile.hasNextLine()) {
String line = numFile.nextLine();
Scanner sc = new Scanner(line);
sc.useDelimiter(" ");
while(sc.hasNextDouble()) {
list.add(sc.nextDouble());
}
sc.close();
}
numFile.close();
System.out.println(list);
}
}
How would I reverse the ArrayList I created? The code I have works, I just have no idea how to reverse it. And where exactly would I put that code? Thanks!
You don't need to reverse the ArrayList, just iterate it in reverse order. Something like,
for (int i = list.size(); i > 0; i--) {
System.out.println(list.get(i - 1));
}
If you must reverse the List before iteration, you might use Collections.reverse(List) like
Collections.reverse(list);
I have created a list of Strings and I need to reverse the order of them using an iterator. This is what I have but it is not working. Can someone please help me find a way to do this or tell me what I am doing wrong?
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class nameList2
{
public static void main(String[] args)
{
List<String> nameList = new ArrayList<String>();
nameList.add("Joey"); //list of Strings
nameList.add("Nicole");
nameList.add("Lucas");
nameList.add("Bobby");
nameList.add("Michelle");
nameList.add("Allie");
Iterator<String> nameIterator = nameList.iterator(); //iterator
for(String s : nameList) //for, each loop
{
if(nameIterator.hasNext()); //compile list
nameIterator.next();
}
for(String s: nameList) //for, each loop
{
if(nameIterator.hasPrevious()); //error states method cannot find hasPrevious?
System.out.println(nameIterator.previous());
}
}
}
use ListIterator instead of Iterator as :
ListIterator<String> list = nameList.listIterator(nameList.size());
// Iterate in reverse.
while(list.hasPrevious()) {
System.out.println(list.previous());
}
and you can do also as Jonk suggested using Collections.reverse(nameList);
Use two ListIterators, one at the start, one at the end and swap until they meet.
And don't forget the edge case(s)
public static void main(String[] args) {
List<String> nameList = new ArrayList<String>();
nameList.add("Joey"); //list of Strings
nameList.add("Nicole");
nameList.add("Lucas");
nameList.add("Bobby");
nameList.add("Michelle");
nameList.add("Allie");
ListIterator<String> list = nameList.listIterator();
System.out.println("Before Reversed\n");
while(list.hasNext()){
System.out.println(list.next());
}
System.out.println("\nAfter Reversed \n");
while(list.hasPrevious()){
System.out.println(list.previous());
}
}