3

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);
}
}
5
  • totalHrsWkd = input.nextDouble(); should be put inside the while loop. Otherwise you're just running in an infinite loop with nothing changing totalHrsWkd Commented Feb 21, 2012 at 20:49
  • Why did you choose to use Scanner? That's almost never appropriate for interactive programs (and causes problems like you see here). Instead, use BufferedReader.readLine. Commented Feb 21, 2012 at 20:50
  • Yeah this is a programing assignment for my Java 1 class. Essentially the teacher provided us with the pseudocode and we needed to code the program. We have only dealt with the Scanner class so far. I haven't even come across BufferedReader.readLine yet in our book. Commented Feb 21, 2012 at 20:54
  • @GregHewgill I guess It's because Scanner is the first thing you learn in college to handle user input. Commented Feb 21, 2012 at 20:55
  • That still doesn't make it appropriate for interactive programs. Commented Feb 21, 2012 at 20:56

3 Answers 3

4

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().

Sign up to request clarification or add additional context in comments.

3 Comments

So then do I need to cast it as a string first? Such as: String ID = System.out.println( "Enter your employee ID: "); int employeeID = Int.parseInt( ID ); If so, I get incompatible type error as well as cant find symbol variable Int.
Not quite, you're trying to assign the return value of println() to a String. Try: String employeeIDStr = input.nextLine(); int employeeID = Integer.parseInt(employeeIDStr); Note also the use of the Integer class instead of Int.
Finally! Really sorry to bother you with beginner problems, but I'm basically left to fend for myself given that she spends class time lecturing over power point slides. I have to write a program from scratch now, so I wanted to get this one done 1st to help serve as a template. Thanks again!
2
attempt = 3; // Seems suspicious

attempt++; // I think it's more appropriate in loops of this kind than ++attempt

Comments

1

The reason why your program doesn't work when totalHrsWkd is less than 0 is because the following code creates an infinite loop.

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

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

You should read the number of hours worked inside this loop as well.

1 Comment

Now I see what was meant above, missing totalHrsWkd = input.nextDouble(); I needed to place that under my second prompt. This solved my infinite loop problem, but still working on the other issue, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.