Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8708361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:07:13+00:00 2026-06-13T04:07:13+00:00

I am trying to have a scanner take input in a loop. Once the

  • 0

I am trying to have a scanner take input in a loop. Once the user wants to finish he can exit this loop. I have tried many different ways to do it but there is always some problem. This is the code:

private void inputEntries() {
    Scanner sc = new Scanner(System.in);
    System.out.println("Continue?[Y/N]");
    while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here
        System.out.println("Enter first name");
        String name = sc.nextLine();
        System.out.println("Enter surname");
        String surname = sc.nextLine();
        System.out.println("Enter number");
        int number = sc.nextInt();
        Student student = new Student(name, surname, number);
        students.add(student);
        try {
            addToFile(student);
        } catch (Exception ex) {
            Logger.getLogger(TextReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Continue?[Y/N]");
    }
}

The problem with the code above, which also happens on different methods I tried, is that when the user types Y, the Scanner will skip the first input for first name,and jump to the surname. If the user types N the loop stops correctly. Someone can explain the reason this happens, and how to overcome using Scanner class?

p.s: Doing something like while(sc.nextLine().equals("Y")), will cause the loop to terminate before getting input from user after first run of the loop.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:07:15+00:00Added an answer on June 13, 2026 at 4:07 am

    This is because you are using Scanner#next method. And if you look at the documentation of that method, it returns the next token read.

    So, when you read user input using next method, it does not read the newline at the end. Which is then read by the nextLine() inside the while loop. And thus, your firstName contains a newline without you knowing.

    So, you should use nextLine() in your while rather than next().

    Similar is the case with nextInt method. It also does not read the newline. So, you can read using readLine and convert it to int using Integer.parseInt. It can throw NumberFormatException if input value cannot be converted to int. So you need to handle it accordingly.

    You can try the below code: –

    Scanner sc = new Scanner(System.in);
    System.out.println("Continue?[Y/N]");
    while (sc.hasNext() && (sc.nextLine().equalsIgnoreCase("y"))) {//change here
        System.out.println("Enter first name");
        String name = sc.nextLine();
        System.out.println("Enter surname");
        String surname = sc.nextLine();
        System.out.println("Enter number");
        int number = 0;
        try {
            number = Integer.parseInt(sc.nextLine());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        System.out.println("Continue?[Y/N]");
    }
    

    But, note one thing, if you enter a value that cannot be passed to Integer.parseInt you will get an exception, and that input will be skipped. For that case, you need to handle it by using while loop.

    Or, if you don’t want to do that exception handling: –

    You can add an empty sc.nextLine() after sc.nextInt(), that will consume the newline left over, like this: –

        // Left over part of your while loop
    
        String surname = sc.nextLine();
        System.out.println("Enter number");
        int number = sc.nextInt();
        sc.nextLine(); // To consume the left over newline;
        System.out.println("Continue?[Y/N]");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to have user input where user types a letter and then depends
I am trying to have my user input not crash my program by restricting
I have been trying to make a clock that the user can set. I
I'm trying to get Java's scanner to keep accepting input until the user types
I have been trying to write a bare-bones ping scanner using Perl for internal
I'm trying to have images fade in with css3 once they're loaded. The problem
I am trying to have a table like this: Manager Expert Adminis. Staff Dept
So I'm trying to have a window that I can drag around by the
I am trying to work on some stuff using the Scanner for input, but
I have this error while trying to run my program. I am still wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.