You should use nextLine and then convert it to the expected types.
In the above script, read the line, then translate it to an integer, because next and nextInt just read the input before the space occurs. Therefore, when you call nextInt , it just consumes the number and leaves the newLine character that will be consumed in nextLine .
From the question, it seems like this is how you are going to read the input.
- The first integer.
- Second line.
- The third line will be two words separated by a space.
This is your code.
Scanner scan = new Scanner(System.in); int x = Integer.parseInt(scan.nextLine()); //read line and then cast to integer System.out.println("p"); String p = scan.nextLine(); System.out.println("qm"); String[] linesParts = scan.nextLine().split(" "); // read the whole line and then split it. String q = linesParts[0]; String m = linesParts[1];
source share