+ 1
6 Answers
+ 4
Scanner input = new Scanner(System.in);
String token = input.next();
names.add("Mozzie");
names.add("Emma");
names.add(token);
Scanner class does not have a nextArrayList method. You can take a string input, or multiple strings if you use a loop, with the next() method.
+ 3
Kim Jonas Cabugayan
You cannot take input as an array or list.
You have to add one by one but Sololearn doesn't support one by one input so can take all input with a seperate lines like:
xyz
abc
while(true) {
String name = input.next();
if (name.equals("stop"))
break;
names.add(name);
}
+ 3
Kim Jonas Cabugayan a much different approach would be to use Arrays instead, you can take input of string in which each elements are separated by a comma(for e.x "Jack,Emma,Tom,John").
Than split() them into a arrays of strings ( e.x input.split(",") ).
Scanner input = new Scanner(System.in);
// Arrays of strings
String[] string=input.next().split(",");
// Use Arrays.asList() method to convert arrays into List
ArrayList<String> names = new ArrayList<String>(Arrays.asList(string));
System.out.println("List of names, " + Arrays.toString(string)+ "\nSize: " + string.length + "\n");
for (String name : names){
System.out.println("*" +name+ "*");
}
+ 1
What is it you want to do?
You need to create the array first. You can add items after that.
+ 1
Ausgrindtube
I'm trying to add element(s) from user.
0
Lets make a group in facebook and do chats ??