1

With a scanner, you use input.nextInt() for an int or input.nextDouble() for a double.

Why do you use input.nextLine() for a String instead of input.nextString()?

4
  • Because the whole line is a string? Then you would just call it a line... Commented Mar 3, 2017 at 4:25
  • I think you could be confused because it takes the whole line (including spaces) excluding any line separator at the end. Commented Mar 3, 2017 at 4:27
  • Yes, I am confused haha, just started to try and learn java. I'm only asking about the specific (Line) in the entire line of String line = input.nextLine(); Commented Mar 3, 2017 at 4:31
  • My question is why wouldn't it be String line = input.nextString(); ? Commented Mar 3, 2017 at 4:35

4 Answers 4

3

The closest thing to nextString() is next(), which returns the next token, which by default is the next "word" (which is controlled by setting the delimiter, that defaults to whitespace).

nextLine() returns the (rest of the) current line (up to but not including the new line char(s)), regardless of the delimiter. That's why it's called line not String, because "line" most accurately describes what it does, rather than what type it returns which wouldn't distinguish itself from next()

Sign up to request clarification or add additional context in comments.

Comments

1

Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.

This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. so:

Why would you call it nextString() if this method returns a line?

9 - Naming Conventions

1 Comment

Thank you, so what it's returning is not a String?
0

Scanner class takes input and tries to convert it into mentioned datatype. If you have written nextDouble then it will try to convert data to Double datatype. If the conversion fails then it will throw an error.

In case of nextLine, it will take the complete next line and convert it to string because string can be n characters long. this is a test message is a string value.

String data can contain spaces whereas any other datatype won't.

For nextLine(), Scanner will match the string with LINE_PATTERN = ".*(\r\n|[\n\r

])|.+$" regex.

For nextDouble() or any other method you pass invalid input then it will throw an exception of java.util.InputMismatchException type.

4 Comments

Thank you I think I understand it now, but if line is a data type why can it be used as the value after String in the input line? Also, why dont you say String instead of Line since it would be a String?
i guess because a String can hold new lines separator too
So a line is within a string? And an int or double, etc. don't have anything else within them, so that's why you just use Int, Double, etc.?
Line could be String. But a string could be multiple lines.
0

To add up on other answers, all native types have fixed size in bytes (i.e. byte is 1, charand short is 2, long and double is 8, ...) so it is easy for Scanner to read just that amount of bytes and return the appropriate result.

Now, how many bytes is a String? If it were C, it would be "until a nul character is found", but nul characters are legal in Java strings so you can't know where to stop reading. That's why there is no nextString() method.

Instead, you could arbitrarily decide to end a string at a specific character, and that character ended up being the newline \r\n combination. The correct name for that method is then nextLine().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.