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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:29:07+00:00 2026-06-08T18:29:07+00:00

Would anyone point me in the right direction, of why when i use a

  • 0

Would anyone point me in the right direction, of why when i use a for loop the println function comes up two times in the output. Thanks

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the number of employees to calculate:");
    int numberEmployees = scan.nextInt();

    for(int i=0; i<numberEmployees; i++){               
            System.out.println("Enter First Name:");
            name = scan.nextLine();
            System.out.println("Enter Last Name:");
            last = scan.nextLine();
            System.out.println("Enter Document #:");
            document = scan.nextInt();
            System.out.println("Enter Basic Salary");
            basicSalary = scan.nextInt();
            System.out.println("Enter # of Hours");
            hours = scan.nextInt();
    }
}

OUTPUT

Enter the number of employees to calculate:
1
Enter First Name:
Enter Last Name:
daniel
Enter Document #:
  • 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-08T18:29:09+00:00Added an answer on June 8, 2026 at 6:29 pm

    The problem is that when you entered 1 with a new line, the nextInt() function doesn’t remove the newline that you had from entering in the 1. Change your calls to scan.nextInt() to Integer.parseInt(scan.nextLine()) and it should behave the way you want.

    To further explain; here’s stuff from the Java API.

    A Scanner breaks its input into tokens using a delimiter pattern,
    which by default matches whitespace. The resulting tokens may then be
    converted into values of different types using the various next
    methods.

    and

    The next() and hasNext() methods and their primitive-type companion
    methods (such as nextInt() and hasNextInt()) first skip any input that
    matches the delimiter pattern, and then attempt to return the next
    token. Both hasNext and next methods may block waiting for further
    input.

    So, what evidently happens (I didn’t see anything on the page to confirm it) is that after next(), hasNext(), and their related methods read in a token, they immediately return it without gobbling up delimiters (in our case, whitespace) after it. Thus, after it read in your 1, the newline was still there, and so the following call to nextLine() had a newline to gobble and did so.

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

Sidebar

Related Questions

Can anyone point me in the right direction on how I would minimize ambient
can anyone please point me in the right direction for a json database on
Would anyone now why this script is disabling my nav in webkit browsers $(document).ready(function
I would like to create a debugging helper function which I can use within
Would anyone know why the bmlabel is not updated? and the log score shows
Would anyone please advise how in jade for nodejs I can truncate a string
Would anyone be able to tell me how to pull the server name out
Would anyone offer tips, links, code snippets on how to browse to a file
Would anyone like to recommend free alternative to SharePoint Portal ? We want to
Would anyone happen to know a trick that will keep this MSBuild task from

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.