+ 2

How to scan multiple string in a single line?

For example there are 2 strings. First one is cat secone one is move. Then the input of the code should be cat move.

4th May 2019, 10:54 AM
REXH
REXH - avatar
3 odpowiedzi
+ 3
Version 4: Scanner scan = new Scanner(System.in); //.next() reads a String until a white space //it reads not the full line String a = scan.next(); String b = scan.next(); System.out.println(a + " " + b); input: cat move output: cat move
4th May 2019, 12:09 PM
Denise Roßberg
Denise Roßberg - avatar
4th May 2019, 11:22 AM
voja
voja - avatar
+ 1
Version 1: Scanner scan = new Scanner(System.in); //read just the line String str = scan.nextLine(); System.out.println(str); input: cat move output: cat move Version 2: Scanner scan = new Scanner(System.in); String a = scan.nextLine(); String b = scan.nextLine(); System.out.println(a + " " + b); input: cat move output: cat move Version 3: Scanner scan = new Scanner(System.in); //scan.hasNext() returns true if there is a next Line while(scan.hasNext()){ String str = scan.nextLine(); System.out.print(str + " "); } input: cat moves along the road output: cat moves along the road
4th May 2019, 12:00 PM
Denise Roßberg
Denise Roßberg - avatar