0

What is the difference between:

String s2 = scan.nextLine(); 

and

String s2 = scan.next() + scan.nextLine(); 
khelwood
  • 46,621
  • 12
  • 59
  • 83
Pengu
  • 23
  • 5
  • 6
    what does the javadoc say? – Reimeus Feb 14 '19 at 14:00
  • 3
    Try it with `Scanner scan = new Scanner("hello world");`. – Andy Turner Feb 14 '19 at 14:04
  • 2
    `next()` - returns the next token of the Scanner object. -> `"hello world" -> "hello"` ; `nextLine()` - returns the string the current line. -> `"hello world" -> "hello world"` – KunLun Feb 14 '19 at 14:05
  • 2
    The documentation is pretty clear about what both methods do. Please be specific and explain what still remains unclear. – Zabuzard Feb 14 '19 at 14:07
  • 1
    `String s2 = scan.next() + scan.nextLine();` could be at some point considered as variant of `String s2 = scan.next(); scan.nextLine();` in which case you may be interested in [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045). – Pshemo Feb 14 '19 at 14:14

2 Answers2

0

Try this code snippet:

    Scanner scanner = new Scanner(System.in);
    String s = scanner.nextLine();
    String s2 = scanner.next()+ scanner.nextLine();

    System.out.println("scanner.nextLine(): "+ s); 
    System.out.println("scanner.next()+ scanner.nextLine(): " + s2); 

Input + Output:

//Input
hello <enter pressed here>
world <enter pressed here>
//Output
scanner.nextLine(): hello
scanner.next()+ scanner.nextLine(): world

nextLine() method uses buffered input to read in a String that the user has entered. Buffered input means that the user is allowed to backspace and change the String until the user hits the Enter key - in this case, this will return the first inputted number.The next() method finds and returns the next complete token from the scanner, i.e. in this case will return the last input value.

vs97
  • 5,519
  • 2
  • 22
  • 35
0

Reg. Scanner javadoc

next() - Finds and returns the next complete token from this scanner.

nextLine() - Advances this scanner past the current line and returns the input that was skipped.

So, with next() basically it reads only first word, only 1st token (string) is being taken (remaining things are stored in the buffer, but nextLine() allows you to read until enter is pressed= whole line.

Difference can be seen if you will try following snippet and try to put combinations of words and sentences:

Scanner sc = new Scanner(System.in);
System.out.println("first input:");
String tmp = sc.next();
System.out.println("tmp: '" + tmp +"'");
System.out.println("second input:");
tmp = sc.next() + sc.nextLine();
System.out.println("2nd tmp: '" + tmp +"'");
}

Inputs and outputs:

first input:
firstWord
tmp: 'firstWord'
second input:
second sentence
2nd tmp: 'second sentence'
//-------------
first input:
first sentencemorewords
tmp: 'first'
second input:
2nd tmp: 'sentencemorewords'

Maybe better explanation comes with direct printing:

Scanner sc = new Scanner(System.in);
System.out.println("first input:");
String tmp = sc.next();
System.out.println("tmp: '" + tmp +"'");
System.out.println("second input:");
System.out.println("next: " + sc.next() +",... nextLine: " + sc.nextLine());

Notice, only first word is handled by first sc.next(), in case of more words any other word will be handled by second sc.next(), but in case of more than 2 words, remaining string will be handled by nextLine

first input:
first second third more words
tmp: 'first'
second input:
next: second,... nextLine:  third more words

So in your program if you need only one word, use sc.next(), if you need to read the whole line, use nextLine()

Community
  • 1
  • 1
xxxvodnikxxx
  • 1,160
  • 2
  • 15
  • 31