---
title: "Java Scanner issue : Why input is skipped when using next(), nextInt() etc. after nextLine()"  
description: "Java Scanner issue : Why input is skipped when using next(), nextInt() etc. after nextLine()"  
author: "Anonymous User"  
published: 2015-10-09  
updated: 2015-10-09  
canonical: https://www.mindstick.com/forum/33502/java-scanner-issue-why-input-is-skipped-when-using-next-nextint-etc-after-nextline  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Java Scanner issue : Why input is skipped when using next(), nextInt() etc. after nextLine()

I am working on a small java [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp), but i am astonished when i saw a weird [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) of scanner [class methods](https://www.mindstick.com/forum/158867/what-is-the-purpose-of-the-self-keyword-in-python-class-methods), If i need to take two inputs : first one [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) value and next a [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp). So i used nextInt() first for integer and then nextLine() for string :[import](https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code) java.util.Scanner;\
\

```
public class ScannerIssue {		public static void main(String[] args){				Scanner sc = new Scanner(System.in);		System.out.println("Enter an integer");		int i = sc.nextInt();		System.out.println("Enter a String");		String j = sc.nextLine();		System.out.println(i);		System.out.println(j);		sc.close();					}}
```

Now the [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) is

```
Enter an integer6Enter a String6
```

[i.e](https://answers.mindstick.com/qa/39877/what-state-was-the-first-to-enter-the-union-i-e-was-the-first-to-ratify-the-united-states-constitution) it skipped the input from nextLine() [method](https://www.mindstick.com/forum/166/webservice-method) and only prompting input once for integer and print its value What is going wrong in this code?\
\

## Replies

### Reply by Anonymous User

Hi Mary,\
I think its kind of a unnoticeable bug in scanner class, here the scanner's nextInt() method doesn't consumes the last newline character of the input , which is then consumed in the next call to scanner nextLine() method.\
We can handle this by **calling a blank nextLine() after nextInt()** to consume the left over new line chaacter

```
int option = input.nextInt();input.nextLine();  // Consume newline left-overString str1 = input.nextLine();
```

We can also do this, by taking both the inputs as String by calling nextLine() method and convert the integer value using **Integer.parseInt(string) method**. For this we need to use **try-catch block** coz Integer.parseInt throws **NumberFormatException** when an invalid argument pass through it. \
\

```
int option = 0;try {    option = Integer.parseInt(input.nextLine());} catch (NumberFormatException e) {    e.printStackTrace();}String str1 = input.nextLine();
```


---

Original Source: https://www.mindstick.com/forum/33502/java-scanner-issue-why-input-is-skipped-when-using-next-nextint-etc-after-nextline

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
