# Reading input from user

**URL:** <https://forum.codewithmosh.com/t/reading-input-from-user/15035>\
**Category:** Java\
**Created:** [September 9, 2022, 2:47pm UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035 "2022-09-09T14:47:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 9, 2022, 2:47pm UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/1 "2022-09-09T14:47:32Z")

</div>

while doing, reading input exercises

code:  
Scanner scanner= new Scanner(System.in);  
System.out.println(“Enter the age:”);  
Byte age= scanner.nextByte();  
System.out.println(“Age is:”+age);  
System.out.println(“enter name:”);  
String name= scanner.next();  
System.out.println(“you’re :”+name);  
System.out.println(“enter role:”);  
String role= scanner.nextLine();  
System.out.println(“role is:”+role);

output:  
Enter the age:  
24  
Age is:24  
enter name:  
praveen  
you’re :praveen  
enter role:  
role is:

I’m not able to enter the role here. i mean not able to give it as input. any help on this pls.

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [September 9, 2022, 4:29pm UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/2 "2022-09-09T16:29:52Z")

</div>

> [@dppraveen](#):
>
> Scanner scanner= new Scanner(System.in);  
> System.out.println(“Enter the age:”);  
> Byte age= scanner.nextByte();  
> System.out.println(“Age is:”+age);  
> System.out.println(“enter name:”);  
> String name= scanner.next();  
> System.out.println(“you’re :”+name);  
> System.out.println(“enter role:”);  
> String role= scanner.nextLine();  
> System.out.println(“role is:”+role);

What I see is you used `nextLine()`.

Why?

I don’t know this method yet but I think it does not do what we’d expect.  
See [API](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()).

Just keep using `next()`.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/f/f9b47d4b1422855b2198a6287c93229fd81dc57a.png)

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 9, 2022, 11:47pm UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/3 "2022-09-09T23:47:18Z")

</div>

`next()` and `nextLine()` have very different behaviors so you cannot just change them out arbitrarily. In particular, the `nextLine` behavior is very nuanced. Here is the Javadoc describing it:

> Advances this scanner past the current line and returns the input that was skipped. 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.
> 
> Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

NOTE: By “current line” it means “wherever the current pointer happens to be” which may include some already buffered content.

The `next()` method will return the next “token” which by default breaks on _any_ whitespace, so you could not enter a full name with a space using the `next` method. Here is its Javadoc:

> Returns the next token if it matches the specified pattern. This method may block while waiting for input to scan, even if a previous invocation of [`hasNext(Pattern)`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Scanner.html#hasNext(java.util.regex.Pattern)) returned `true`. If the match is successful, the scanner advances past the input that matched the pattern.

So when the `next()` method is called, it also has to read the whole line up to the next newline so something like “praveen\n” is put into the buffer (where “\n” is the newline). It uses the newline as a delimiter for the token that it returns so it returns “praveen” leaving the newline in the buffer “\n”. Then when `nextLine()` is called, the already buffered content already contains a new line so it just returns everything preceeding that newline (which is nothing) without grabbing anything else from `System.in` (emptying the buffer).

It is easier to see if you wrapped the “role” printed out with single quotes:

```java
Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
Byte age = scanner.nextByte();
System.out.println("Age is: " + age);
System.out.print("Enter name: ");
String name = scanner.next();
System.out.printf("You are: '%s'%n", name);
System.out.print("Enter role: ");
String role= scanner.nextLine();
System.out.printf("Role is: '%s'%n", role);

```

If you want to clear out that buffer, you need to call `readLine()` a second time so that we get past the buffered newline and then it will block until you get new input through `System.in`. To keep it clear what we are doing, we can clear the buffer directly after we get the name:

```java
Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
Byte age = scanner.nextByte();
System.out.println("Age is: " + age);
System.out.print("Enter name: ");
String name = scanner.next();
scanner.nextLine();
System.out.printf("You are: '%s'%n", name);
System.out.print("Enter role: ");
String role= scanner.nextLine();
System.out.printf("Role is: '%s'%n", role);

```

This prints out the expected results.

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [September 11, 2022, 1:03pm UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/4 "2022-09-11T13:03:39Z")

</div>

Thanks for the precisions Jason.

Man I will regret my simple `Console.ReadLine();` from C#. 😅

I was just doing some exercices and took the occasion to try those things;

Actually it did not seem to be problematic for what I did.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/c/cabd4b15348effe8963f757fe02cab718ced6579.png)

Could you provide a practical example where it may behave in unexpected way? That would be much appreciated.

Regards.

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 11, 2022, 2:12pm UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/5 "2022-09-11T14:12:19Z")

</div>

I think what this topic shows is a prime example of where Scanner can be confusing. All of the other `next` methods read only one token, but when getting content from the command line they may buffer additional content. If you always use `nextLine` and parse the content yourself then you would not notice this, but if you mix other `next` methods you can see unexpected behavior because you expect that it will request new data from the command line (which is not necessarily the case). As such, Scanner does not pass the principle of least surprise (that is to say, the behavior is not the most obvious thing).

Note: Scanner is a bit less surprising when reading data from a file source.

---

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 12, 2022, 8:34am UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/6 "2022-09-12T08:34:29Z")

</div>

Hi Jason, i kinda got confused. could u pls brief the difference of next() and nextLine() with a example.

it would help me understand better.  
thanks 🙂

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 12, 2022, 2:26pm UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/7 "2022-09-12T14:26:04Z")

</div>

The basic idea is that `next()` returns the next “token” (string of non-whitespace characters) that it finds (skipping any initial whitespace and stopping when it finds another whitespace or the end of the String, whichever comes first). `nextLine()` returns everything from the current position (or marker) - including whitespace - until it finds a newline character (or the end of the String, whichever comes first).

To make this easier to reason about, let us take the console out of it and imagine we are just reading from a file with the following contents:

```nohighlight
Hello, my name is Bond
James Bond

```

If we were to put that content into a file called source.txt we could pass it to a Scanner like this:

```java
try (var scanner = new Scanner(new FileInputStream("source.txt"))) {
  ...
}

```

NOTE: I am using a try-with-resources block since Scanner is AutoCloseable to make sure we close the resource at the end of the block.

Calling the Scanner’s `next()` method would return the String `"Hello,"` and leave a marker after `"Hello,"` (including the space). In contrast, calling the Scanner’s `nextLine()` method will return the String `"Hello, my name is Bond"` and leave a marker at the start of the second line (dropping the newline character).

Now things get interesting when we see what happens if we start mixing and matching. If we call `next()` once and then `next()` again, we will get `"my"` for the second string with a marker after `"my"` (including the space). In contrast, if we call `next()` and then `nextLine()` we get `" my name is Bond"` (starting with the space after `"Hello,"`) for the second string with a marker at the start of the second line (dropping the newline character).

Now let us suppose we get through all of the tokens on the first line by calling `next()` five times to pick up the strings `"Hello,"`, `"my"`, `"name"`, `"is"` and `"Bond"` while leaving the marker at the end of the first line before the newline character. Calling `nextLine()` will return an empty string (`""`) because the Scanner looks for the next newline character and returns everything preceeding it (which is an empty string since the very next character is a newline character). Try it yourself if you are interested to see the result:

```java
try (var scanner = new Scanner(new FileInputStream("source.txt"))) {
  System.out.println("'"+scanner.next()+"'");
  System.out.println("'"+scanner.next()+"'");
  System.out.println("'"+scanner.next()+"'");
  System.out.println("'"+scanner.next()+"'");
  System.out.println("'"+scanner.next()+"'");
  System.out.println("'"+scanner.nextLine()+"'");
}

```

What gets tricky when `System.in` is the InputStream for our Scanner is that we _must_ type a newline character to return control to the program and finish sending input. So input at the command line always ends with a newline character. Methods like `nextByte()` and `next()` can basically ignore the newline character because they are looking for the next token and will skip over intervening whitespace (including newline characters). The `nextLine()` method is just looking for the next newline character after the marker (which is placed right after the previous token that was found). With that in mind, take a look at my previous answer and see if you now understand the behavior of your program.

---

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 16, 2022, 3:44am UTC](https://forum.codewithmosh.com/t/reading-input-from-user/15035/8 "2022-09-16T03:44:40Z")

</div>

Thank u Jason, it was a crystal clear explanation.
