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 647769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:45:32+00:00 2026-05-13T21:45:32+00:00

As an exercise for my Java course in Uni this morning, I had to

  • 0

As an exercise for my Java course in Uni this morning, I had to write a small program to ask the user to input some details, then print them back. I’ve since finished writing it, but I ran into a strange problem along the way.

See the code below:

import java.util.Scanner;

public class Scanner_Exercise
{
    public static void main (String[] args)
    {
        Scanner keyboardIn = new Scanner(System.in);

        int accountId;
        String accountName;
        float accountBalance;

        System.out.println("Account ID: "); //Line 13
        accountId = keyboardIn.nextInt(); //Line 14

        System.out.println("Account name: "); //Line 16
        accountName = keyboardIn.nextLine(); //Line 17

        System.out.println("Account balance: ");
        accountBalance = keyboardIn.nextFloat();
    }
}

When this program runs, line 17 (refer to comments) is skipped; Account name: is printed, but the user is not given the opportunity to enter the information, as if that line of code was commented out. No errors are thrown. The output looks like this:

Account ID:
2
Account name:
Account balance:

However, if I switch lines 13 and 14 with 16 and 17, like as follows, the program runs fine, and no lines are skipped.

System.out.println("Account name: "); //Line 16
accountName = keyboardIn.nextLine(); //Line 17

System.out.println("Account ID: "); //Line 13
accountId = keyboardIn.nextInt(); //Line 14

Why is the line 17 being skipped in the first case, but not the second?

If it’s somehow relevant, I am using JDK 6 Update 18, and TextPad 5.3.1.

  • 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-05-13T21:45:32+00:00Added an answer on May 13, 2026 at 9:45 pm

    A look at the javadoc should show you why.

    Scanner.nextLine() skips to the end of the current line, and returns whatever it skipped. Since the user has already input some data, this function will return what ever is left in the input stream. When you switch the order around, it doesn’t yet have any input, which is why you see it ask for input.

    The important part:

    This method returns the rest of the current line, excluding any line separator at the end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.