I am trying to write a program that reads words from the user until they enter the word “quit”, at which point it prints out all the words they entered on one line separated by commas.
I previously got the last user input (quit) to be only outputted, but now I am lost. Help. There is some commented out code. Thanks. I am new to Java but know C/C++ and UE4.
public static void main(String[] args) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
String[] array = new String[1000];
for (int i=1; i>0; i++) {
System.out.print("Enter string: ");
array[i] = scanner.nextLine();
if (array.equals("quit")) {
System.out.println(array);
break;
}
}
}
array[i].equals("quit")and loop is faultyquit, it's better to use awhileloop rather than making an infinite for loop.System.out.println(Arrays.toString(array))instead.iwhen it's not needed. Simply, omit the third part like sofor(int i = 1; i>0;) {...}ioverflows.