24th Oct 2022, 9:31 AM
Mozzie
Mozzie - avatar
6 Antworten
+ 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.
24th Oct 2022, 11:09 AM
Tibor Santa
Tibor Santa - avatar
+ 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); }
24th Oct 2022, 11:05 AM
A͢J
A͢J - avatar
+ 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+ "*"); }
25th Oct 2022, 4:05 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
What is it you want to do? You need to create the array first. You can add items after that.
24th Oct 2022, 9:41 AM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Ausgrindtube I'm trying to add element(s) from user.
24th Oct 2022, 9:52 AM
Mozzie
Mozzie - avatar
0
Lets make a group in facebook and do chats ??
26th Oct 2022, 5:30 AM
sugam karki
sugam karki - avatar