0
How do I input Strings in an ArrayList and terminate the input when I input -1
Those who are massive in Java, Help
2 odpowiedzi
+ 1
Something like this maybe, if i understood you right
Scanner scan = new Scanner(System.in);
String input = "";
ArrayList<String> list = new ArrayList<>();
while(!input.equals("-1")){
System.out.println("Enter word into array: ");
input = scan.nextLine();
list.add(input);
}
list.remove(list.size()-1); // We use this to remove the -1 from the arrarlist because
// you only use it to breake the while loop
System.out.println(list);
// Dont forget to import
+ 1
If you want the words on a own row instead of [oki, poki, doki]
use a loop:
for(String l: list)
System.out.println(l);