Results 1 to 17 of 17
  1. #1
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default Help with my code

    I am a complete new student to java programming, taking the courses in school (online, only option for me :( )

    For some reason this assignment is kicking my butt, and I cannot figure out what I am doing wrong. I have 3 books, video tutorials, and googled it every way I know how to ask hehe.

    Here is the assignment:

    The program should prompt user for a name. (works)
    Ask for hours worked. (works)
    Ask for hourly wage. (works)
    Display employee name and weekly wage. (works)

    Should also

    Continue to prompt user for a name after one has been figured (doesnt work)
    Prompt user to input positive numbers if a negative is entered (doesnt work)
    End the program if "stop" is entered (works)

    The problem I am running into is that once the user goes through the program it loops back and skips asking for a name again (eliminating a possible stop command in the process), and goes straight to asking for hours. This is what is returned:

    Welcome to Payroll manager!
    Please enter stop when you are finished

    Please enter the Employee's Name:Erik
    Now enter the number of hours Erik has worked:10
    Now enter Erik's hourly wage as a positive number:$10
    Erik will get paid 100.0 this week.

    Welcome to Payroll manager!
    Please enter stop when you are finished

    Please enter the Employee's Name:Now enter the number of hours has worked:


    Also if at any time a negative number is entered into a wage or hours field, it just stops, this is returned:
    Welcome to Payroll manager!
    Please enter stop when you are finished

    Please enter the Employee's Name:Erik
    Now enter the number of hours Erik has worked:-1



    No errors, it just stops.

    Netbeans shows no errors, and it compiles with no errors. So I dont know what I am doing wrong.

    Here is my code:
    Java Code:
    package payrollcp;//This is the project for the payroll checkpoint.
    
    import java.util.Scanner;//This imports the utility "Scanner" for use by the program.
    
    
    
    public class Payrollcp { //This is the main class of the program
    
        
        public static void main(String[] args) { //This is the main method
    
            Scanner input = new Scanner(System.in); // This tells the program to accept input from the user.
            
    
    
            
    
            String name; // The variable for the Employee's Name
            double hours; // The variable for amount of hours that the employee works.
            double wages; // The variable for the amount the employee makes per hour.
            double weeklypay; // The variable for the weekly pay for the employee.
    
            boolean stop = false;
            while (!stop){
    
    
            System.out.println("Welcome to Payroll manager!");//A welcome message for the user.
            System.out.println("Please enter stop when you are finished");
            System.out.println(""); //A blank line to seperate from the work area for the user, for aesthetics.
            
    
    
            System.out.print("Please enter the Employee's Name:");//Prompts user for the employee's name.
            name = input.nextLine();//Accepts the input of the employee's name.
              if(name.equals("stop")){ //If statement to end the program
                  System.out.println("Thank you for using payroll manager!");// Ending program line.
                stop = true;}// stops the program
            
            
            else//Continues on if "stop" is not entered.
                
                
           System.out.print( "Now enter the number of hours " + name + " has worked:");//Prompts user for the number of hours worked.
           hours = input.nextDouble();//Accepts the input from the user, hours can be in decimal format.
                while(hours <=0.0)// If hours are negative prompts user to input a positive number.
                {
                System.out.print ( "Please enter a positive number" + "Please enter the number of hours " + name + " has worked:");// prompt for positive number.
                }
           
           
            System.out.print("Now enter " + name + "'s hourly wage as a positive number:$");//Prompts user for the hourly pay of the employee, inserts employees name into the "name" field.}
            wages = input.nextDouble();//Accepts input from the user, can be in decimal format.
            while(wages <=0.0)// if wages are negative, prompts user to input a positive number.
                {
                System.out.print("Wages must be a positive number, please enter a positive number:$");// prompt for positive number.
                }
            
            
            weeklypay = hours * wages;//Calculates the weekly pay for the employee, multiplying hours by hourly wage.
            System.out.println(name + " will get paid " + weeklypay + " this week."); //Displays the employee's name and the final amount they will get paid for the week.
            System.out.println("");// blank line to break up the while loop
    
           
        }// end while
    
            }// end class
    }// end program
    Thank you for any help at all!
    Last edited by erikjd21; 04-09-2011 at 09:30 AM. Reason: Was rude, and forgot to thank anyone willing to help :)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    The most common problem is when you input a number value (Scanner.nextInt(), Scanner.nextDouble() etc) you can't get the next String immediately because there is an extra line break. Take the following input for example:

    John 40 1000.00[LINE BREAK]
    Doe 30 1500.00[LINE BREAK]

    now, calling Scanner.nextLine() would firstly take the whole first line including [LINE BREAK]
    calling Scanner.nextLine() again would take the whole second line.

    The problem arises when you call Scanner.nextDouble(), take this for example:
    40 1000.00[LINE BREAK]
    John[LINE BREAK]

    Scanner.nextDouble() would take 40
    Scanner.nextDouble() again would take 1000.00

    then you need to call Scanner.nextLine() to eat the [LINE BREAK]
    then again, you can call Scanner.nextLine() to eat the next line

    So thats probably why it doesn't work the second time around, the name input part just takes the previous line break, and the double tries to take the String name

    if you want to make a negative number always positive, use Math.abs(number).

    Math.abs(-10) = 10
    Math.abs(10) = 10
    Math.abs(-n) = n
    Math.abs(n) = n

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    using while before the condition waged <= 0.0 and hours <= 0.0
    will get you into an infinite loop, since it will always type you println and then find again that it is still negative, this is the first thing, another is that the else
    if(name.equals("stop")){ //If statement to end the program
    System.out.println("Thank you for using payroll manager!");// Ending program line.
    stop = true;}// stops the program


    else//Continues on if "stop" is not entered.
    you have to rap its code with a { to perform them better

  4. #4
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    Thank you for the reply, I will try to figure something out with the scanner input.

    As far as the numbers, I am attempting to make the program loop that segment until the user inputs a positive number, not change the number to a positive one.

    Does that make sense, or am I wording it wrong?

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    baf, i think the idea IS to get an infinite loop, and then only to stop it if "stop" has been input, so i don't think thats a problem in this case.

    although it would've been more ideal to write something like this:

    while (true) {

    }

    to show that you intended an infinite loop.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    erik, then you need a second while loop like this:

    //2nd while loop

    int positiveNumber = Scanner.nextInt();
    boolean numIsNegative = positiveNumber < 0;
    while (numIsNegative) {
    //ask again
    }

    edit:
    a better method though, would be to use a DO-WHILE loop instead considering the data must be input once first, so you could use this instead:

    Java Code:
    int positiveNumber;
    boolean numIsNegative;
    System.out.println("Enter next integer:");
    do {
        positiveNumber = Scanner.nextInt();
        numIsNegative = positiveNumber < 0;
        if (numIsNegative) {
            System.out.println("Sorry, positive numbers only.");
            System.out.println("Try again:");
        }
    } while (numIsNegative);
    Last edited by ozzyman; 04-09-2011 at 10:09 AM.

  7. #7
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    yeah i know about the infinite loop for the whole thing, but i meant over here :
    hours = input.nextDouble();//Accepts the input from the user, hours can be in decimal format.
    while(hours <=0.0)// If hours are negative prompts user to input a positive number.
    {
    System.out.print ( "Please enter a positive number" + "Please enter the number of hours " + name + " has worked:");// prompt for positive number.
    }
    if you run the program it will just keep on printing ("Please enter a positive number" + "Please enter the number of hours " + name + " has worked:") and you can't be given a chance to write a positive number, or am i wrong about that ?

  8. #8
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    I run it on my BlueJ and that was what happened, if NetBeans works differently then this is another case, and hope to be acknowledged about that.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    no you are right, that is an infinite loop, but it should be, but there also should be some clause to get out of it, like the solution i posted for erik before that post.

  10. #10
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    yeah I just saw it, and understood what you mean by it being an infinite loop, YOu need to be infinite when entering negative numbers but not when printing the command
    System.out.println("Sorry, positive numbers only.");
    System.out.println("Try again:");

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    those commands are only printed if the number is negative, and its necessary because System.nextInt() will prompt the user for another input, so they need to know that they are expected to input a number for the same item again

  12. #12
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by baf06 View Post
    using while before the condition waged <= 0.0 and hours <= 0.0
    will get you into an infinite loop, since it will always type you println and then find again that it is still negative, this is the first thing, another is that the else


    you have to rap its code with a { to perform them better
    I changed the while statements to if statements, and although it returns a value now, its not looping correctly, I am missing something.

    Also, I have the { braces in the code you quoted, are they misplaced?

    Next, looking through all of my books, I only see the nextLine methods for using the Scanner import. What should I use to fix that area of the program.

  13. #13
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    I don't know exactly about the Netbean but it usually reads one line after the else if you didn't embrace the code after it, you should put braces after the else and till the last thing you want to perform if the name is not stop

  14. #14
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by erikjd21 View Post
    I changed the while statements to if statements, and although it returns a value now, its not looping correctly, I am missing something.

    Also, I have the { braces in the code you quoted, are they misplaced?

    Next, looking through all of my books, I only see the nextLine methods for using the Scanner import. What should I use to fix that area of the program.
    you're probably missing a loop? have you tried the solution i gave you?

  15. #15
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    HOLY HELL YOU GUYS FIXED IT

    Thank you sooo much!!

    If you guys lived anywhere near me, Id buy you all a beer!

    Thank you again!

    @Ozz

    It took some figuring on what I was doing, but I got your solution to work, you really helped me. All of you did. You guys have no idea how greatful I am!

  16. #16
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    it is our pleasure :) this is what this website is about, help whenever you can :P

  17. #17
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    For anyone who wants to see the end result (I havent commented everything yet, but here is the code of the working product:

    Java Code:
    package payrollcp;//This is the project for the payroll checkpoint.
    
    import java.util.Scanner;//This imports the utility "Scanner" for use by the program.
    
    
    
    public class Payrollcp { //This is the main class of the program
    
        
        public static void main(String[] args) { //This is the main method
    
            Scanner input = new Scanner(System.in); // This tells the program to accept input from the user.
            
    
    
            
    
            String name; // The variable for the Employee's Name
            double hours; // The variable for amount of hours that the employee works.
            double wages; // The variable for the amount the employee makes per hour.
            double weeklypay; // The variable for the weekly pay for the employee.
    
            boolean stop = false;
            while (!stop){
    
    
            System.out.println("Welcome to Payroll manager!");//A welcome message for the user.
            System.out.println("Please enter stop when you are finished");
            System.out.println(""); //A blank line to seperate from the work area for the user, for aesthetics.
            
    
    
            System.out.print("Please enter the Employee's Name:");//Prompts user for the employee's name.
            name = input.next();//Accepts the input of the employee's name.
            System.out.print("");
                    if (name.equals("stop"))
                    { //If statement to end the program
                  System.out.println("Thank you for using payroll manager!");// Ending program line.
                stop = true;// stops the program
                }
    
    
            else//Continues on if "stop" is not entered.
    
    
           System.out.print( "Now enter the number of hours " + name + " has worked:");//Prompts user for the number of hours worked.
          
         
         boolean negHours;
         do {
             hours = input.nextDouble();
             negHours = hours < 0;
             if (negHours) {
                 System.out.print("Please enter a positive number:");}
         } 
         while (negHours);
    
    
            System.out.print("Now enter " + name + "'s hourly wage as a positive number:$");//Prompts user for the hourly pay of the employee, inserts employees name into the "name" field.}
          
    
         boolean negWages;
         do {
             wages = input.nextDouble();
             negWages = wages < 0;
             if (negWages) {
                 System.out.print("Please enter a positive number:");}
         }
         while (negWages);
    
            weeklypay = hours * wages;//Calculates the weekly pay for the employee, multiplying hours by hourly wage.
            System.out.println(name + " will get paid " + weeklypay + " this week."); //Displays the employee's name and the final amount they will get paid for the week.
            System.out.println("");// blank line to break up the while loop
    
           
             }// end while
    
         }// end class
    // end program
        }

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  3. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •