0

How do I input Strings in an ArrayList and terminate the input when I input -1

Those who are massive in Java, Help

6th Mar 2019, 6:47 PM
Donald Mbara
Donald Mbara - avatar
2 Respostas
+ 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
6th Mar 2019, 8:39 PM
JavaBobbo
JavaBobbo - avatar
+ 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);
6th Mar 2019, 8:57 PM
JavaBobbo
JavaBobbo - avatar