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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:07:40+00:00 2026-05-30T07:07:40+00:00

Hi I am having trouble with getting my program to run properly. I was

  • 0

Hi I am having trouble with getting my program to run properly. I was able to clear any syntax errors, but now I am having issued with my output.

First of all, inside the first IF statement, it prompts for the person to enter their name and department at the same time, so that when it outputs, name is blank and only department has input. I’m thinking it’s something with the whole IF statement, because if I change “String name” to input.next then name prompts correctly, but dept and totalHrsWkd get merged together.

Also, while testing my program, it crashes when I enter a negative number for totalHrsWkd. It will display the two print statements all on one line and then crash JCreator.

I’d appreciate any help on the matter, thank you!

    public static void main(String[] args)
    {
    // TODO code application logic here

    int attempt = 1, employeeID = 0;
    double hoursWorked = 0.0;
    double overtimeHrs = 0.0;
    double totalHrsWkd = 0.0;

    Scanner input = new Scanner(System.in);

    while( attempt < 4 )
    {
        System.out.println( "Enter your employee ID: " );
      employeeID = input.nextInt();

        if( employeeID == 12345678 )
        {
        System.out.printf( "Enter your name: " );
        String name = input.nextLine();

    System.out.printf( "Enter your department: " );
        String dept = input.nextLine();

    System.out.printf( "Enter your hours worked including overtime:  " );
        totalHrsWkd = input.nextDouble();

        while( totalHrsWkd < 0 )
            {
            System.out.printf( "Try again!  Hours worked cannot be negative.");

            System.out.printf( "Enter your hours worked including overtime: ");
            }

            overtimeHrs = totalHrsWkd - 40;
            hoursWorked = totalHrsWkd - overtimeHrs;

            if( overtimeHrs <= 0 )
            {
            }
            else if( overtimeHrs == 0 )
            {
            }
            else if( hoursWorked == totalHrsWkd )
            {
            }
            else if( hoursWorked == 40 )
            {
            }
        System.out.printf( "Name: %s\n" + "Dept: %s\n" + 
                    "Hours Worked:  %.2f\n" + "Overtime Hours: %.2f\n"
                     + "Total Hours Worked: %.2f\n", name, dept, 
                     hoursWorked, overtimeHrs, totalHrsWkd);

        attempt = 3;
        }
    else
    {
        if(attempt < 3)
        {
        System.out.println( "Invalid ID!  Try again."  );
        }
        else
        {
        System.out.println( "Invalid ID!  0 attempts left.  Exiting program!" );
        }

      }

    ++attempt;
   }
    System.exit(0);
}
}
  • 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-30T07:07:41+00:00Added an answer on May 30, 2026 at 7:07 am

    The problem is that

      employeeID = input.nextInt();
    

    only reads the next sequence of digits from the input stream. However, in order to actually type the employee ID, you had to press Enter too. The Enter keypress is still waiting on the input, so when you next call

        String name = input.nextLine();
    

    the Scanner class goes “oh, here’s an Enter keypress, I guess the user wanted an empty name.” You’re not even given a chance to enter a name, which is why it seems like your program asks two questions (the name and department) at the same time.

    If you wish to continue to use the Scanner class, I would recommend avoiding the methods such as nextInt() and always using nextLine() instead. You will, of course, then have to convert the number typed by the user into a Java integer using something like
    parseInt().

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

Sidebar

Related Questions

I'm having trouble getting my program to read this XML file properly, it will
I am having trouble getting my class to from program.cs to run in form.cs
For some reason I'm having trouble getting my program to run a while loop.
I'm having trouble getting a sample program to link correctly (in this case against
I am having trouble getting SendMessage to activate a menu on another program. When
I'm having trouble getting a rotary encoder to work properly with AVR micro controllers.
I'm having trouble getting the following to work in SQL Server 2k, but it
I'm having trouble getting the GPS's onLocationChanged to run on a different thread. I
I am learning python and am having trouble getting this program to work correctly.
I'm having trouble getting a bit to work properly in java6...I'm trying to write

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.