So below is my code. I'm trying to use the Scanner class to read an input from the console, and then print out each token on a separate line. But there's a small problem where the user has to input twice. An example of what happens is given below the code
import java.util.Scanner;
public class StringSort {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = scanner.nextLine();
int i = 0;
while (i< str.length()) {
i++;
System.out.println("" + scanner.next());
}
scanner.close();
}
}
Example:
Enter a string:
what is love
what is love
what
is
love