Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Programming Loop Execution Problem

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Java Programming Loop Execution Problem [Solved/Resolved]

    Hey guys,

    I am most definitely new here and new to Java. I have been trying to get help with my code skipping executing a specific function when I run the program. The first time through, everything executes perfectly, that is until the loop goes into effect. It skips over allowing the user to input "Employee Name" which is extremely important, because this is the are where the user can input "stop" to exit. Anyone know a different method to use or where a conflict could be occuring to cause this problem?

    Here's the code:

    package payrollprogram2;
     
    import java.util.Scanner; 
     
    public class PayrollProgram2 
    {
     public static void main(String[] args) 
     {
      Scanner input = new Scanner(System.in);
     
     
         System.out.printf("Welcome to the Employee Payroll System!\n");
     
      String name; 
      double rate; 
      int hours; 
      boolean g = true;
     
    while (g == true)
      {  
       System.out.printf("Enter employee's name. Type 'stop' to exit.\n");
       name = input.nextLine();
     
       if(name.equals("stop")) 
       {System.out.printf("Exiting. Good-bye!");
       g = false;
        break;}
     
       else
       {
        do 
        {
         System.out.printf("Enter the hourly rate.\n");
         rate = input.nextDouble();
     
         if(rate <= 0)
         System.out.printf("Please enter a positive number (above zero).\n");
     
        }while(rate <= 0);
     
        do
        {
         System.out.printf("Enter the amount of hours worked (Whole, positive numbers only).\n");
         hours = input.nextInt();
     
         if(hours <= 0)
         System.out.printf("Please enter a positive number (above zero).\n");
     
        }while(hours <= 0);
            int overtime;
            if (hours <= 40)
                {
                    overtime = 0;
                }
            else
                {
                    overtime = hours - 40;
                }
     
        double payroll = hours * rate + overtime * rate/2;
     
              System.out.printf("\nEmployee Name: %s\n", name);
              System.out.printf("Hours Worked: %d\n", hours);
              System.out.printf("Rate per Hour: $%.2f\n", rate);              
              System.out.printf("Total Income: $%.2f\n", payroll);
       }
      }
     }
    }
    Last edited by Advent; April 14th, 2012 at 04:37 PM. Reason: Solved/Resolved


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Java Programming Loop Execution Problem

    This is actually a pretty quick fix, but the reason the problem is occurring can be illusive. The methods nextInt(), nextDouble(), nextBoolean()...etc. don't read anything more than what they need to. For instance, if a double is to be read and you type 5.56 and hit enter then the double 5.56 is read correctly but not the newline character (caused by enter being pressed) which is left in the input buffer. Thus, if you try to read the line afterwards it will see the new line character left over in the buffer and return a empty string. To rectify this you need to call nextLine() which will read the unwanted newline character and set the position at the beginning of the next line.

  3. The Following User Says Thank You to KucerakJM For This Useful Post:

    Advent (April 14th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Programming Loop Execution Problem

    Quote Originally Posted by KucerakJM View Post
    This is actually a pretty quick fix, but the reason the problem is occurring can be illusive. The methods nextInt(), nextDouble(), nextBoolean()...etc. don't read anything more than what they need to. For instance, if a double is to be read and you type 5.56 and hit enter then the double 5.56 is read correctly but not the newline character (caused by enter being pressed) which is left in the input buffer. Thus, if you try to read the line afterwards it will see the new line character left over in the buffer and return a empty string. To rectify this you need to call nextLine() which will read the unwanted newline character and set the position at the beginning of the next line.
    I got it from what you said, I was able to research more thoroughly and found others instances that said to call input.nextLine(); directly after my last input.nextInt();

    Thank you so very much!!!!
    Last edited by Advent; April 14th, 2012 at 04:36 PM. Reason: Found it =) Help was extremely appreciated!

Similar Threads

  1. Replies: 3
    Last Post: March 28th, 2012, 08:47 AM
  2. Java applet programming problem
    By danielparry in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 4th, 2011, 10:31 AM
  3. java threading execution time question
    By centenial in forum Threads
    Replies: 4
    Last Post: September 8th, 2010, 11:32 PM
  4. Java mailer programming code problem
    By shadeslayer88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 01:21 AM
  5. Java GUi program Execution
    By Rajan in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 08:48 PM