Results 1 to 8 of 8
  1. #1
    studentone is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Need assistance with real numbers

    Hello,
    I am very new to programming and I am having a little trouble with my homework assignment. Everything runs correctly until I enter a real number in one of my fields. I have to make a payroll program that the user can enter the employees name, hours worked, and hourly pay rate. If I enter 10 as the hourly pay rate the program is successful. If I enter 9.75 then I get an error. I was thinking my problem is with the scanner object but not sure. Below is the progam I have so far. Any help is appreciated.

    Java Code:
    package payroll1;
    
    import java.util.Scanner;
    public class Payroll1
    {
        public static void main(String[] args) {
             // Create way to read input from the user
           Scanner input = new Scanner( System.in );
    
           int number1; // hours worked
           int number2; // hourly pay rate
           int sum; // total of hours worked multiplied by hourly pay rate
           
           //Display Program name
           System.out.println( "Payroll Part 1!" );
    
           //Read user input from the console
           System.out.println( "Enter Employee's Name" ); // prompt
           input.nextLine(); // read a line of text
         
    
           System.out.println( "Enter hours worked"); // Prompt
           number1=input.nextInt(); // Read first number from user
    
    
           System.out.println( "Enter hourly pay rate" ); // prompt
           number2=input.nextInt(); // read second number from user
    
           sum=number1*number2;// hours worked * hourly pay rate
           System.out.printf("Weekly Pay is $%d\n", sum);
               }
        // display employee's name and weekly pay total
         // end method main
    // end Class payroll
        }
    Last edited by pbrockway2; 11-13-2011 at 01:20 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need assistance with real numbers

    If I enter 9.75 then I get an error.
    In general, you should describe the error: ie copy and post the entire stacktrace that you get at runtime and say which lines of your code it refers to. This will help people understand what is going on.

    In this case, however, the problem comes from the fact that you say

    Java Code:
    int number2; // hourly pay rate
    This declares number2 as an int - ie it can only hold integer values. 10 is OK, but 9.75 is not.

    Later you give this variable a value with

    Java Code:
    number2=input.nextInt(); // read second number from user
    Again the nextInt() will read an integer value from the user - and will get very upset if what the user types is not an integer value.

    Basically you are going to have to change the type of number2 so that it will hold real numbers. And you are going to have to change the scanner method you use from nextInt() to something that will read real values. These changes may have an effect on other variables within the program.

    (Also please use code tags when posting code. You put [code] at the start of the code and [/code] at the end. This will means the code ends up properly formatted.)

  3. #3
    studentone is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Need assistance with real numbers

    I apoligize. Here is the error I recieve:

    run:
    Payroll Part 1!
    Enter Employee's Name
    John Doe
    Enter hours worked
    80
    Enter hourly pay rate
    9.75
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at payroll1.Payroll1.main(Payroll1.java:27)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 12 seconds)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need assistance with real numbers

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at payroll1.Payroll1.main(Payroll1.java:27)
    OK - so you read these stack traces from the top until you reach a line referring to your code. This will tell you were to begin looking for the cause of the problem. In this case it is line 27 of Payroll1.java where you assign to the number2 variable using the scanner's nextInt() method. As I said before, nextInt() is no good if you are wanting to read a real number. Look in the Scanner API docs for a more useful method.

  5. #5
    studentone is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Need assistance with real numbers

    Thanks so much!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need assistance with real numbers

    You're welcome.

  7. #7
    studentone is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Need assistance with real numbers

    I just got it going. It was a simple fix after all lol. I just rewrote line #27 to number2=(int) input.nextDouble(); // read second number from user. The more I do these the programs the more I understand!
    Again thanks for pointing me in the right direction!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Need assistance with real numbers

    I just rewrote line #27 to number2=(int) input.nextDouble(); // read second number from user
    Check that it gives the right answer for the week's pay.

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  2. Assistance about this.
    By Syfer in forum Advanced Java
    Replies: 38
    Last Post: 06-21-2010, 05:59 PM
  3. Looking for assistance
    By s_dawg101 in forum New To Java
    Replies: 32
    Last Post: 11-04-2009, 02:49 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •