Results 1 to 7 of 7
  1. #1
    smaster is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Need help with do while loop

    Here is my problem, at the end of my do while loop when I ask if you want to "Enter a new emplyee" it doesn't read my input and jumps out of the loop. Here is my code:
    Java Code:
            do{
                
            System.out.print("Employee ID: ");
            employeeID = keyboard.nextInt();
            keyboard.nextLine();
      
            System.out.print("Employee Name: ");
            employeeName = keyboard.nextLine();
            
            System.out.print("Enter what kind of shift (1 for days, 2 for nights): ");
            shift = keyboard.nextInt();
            
                do{
                    System.out.print("Employee type (1,2, or 3): ");
                    employeeType = keyboard.nextInt();
                
            
               
                    if (employeeType < 1 || employeeType > 3){
                
                        System.out.println("Error, Type must be 1,2, or 3\n");
                        
                    }
                
                
                }while (employeeType < 1 || employeeType > 3);
           
                switch(employeeType)
                {
               
               
                    case 1:
                        System.out.print("Hours Worked: ");
                        hrsWorked = keyboard.nextDouble();
                        System.out.print("Enter rate of pay: ");
                        rOP = keyboard.nextDouble();
                    
                        ProductionWorker pw = new ProductionWorker(employeeName, employeeID, shift, rOP, hrsWorked);
                        totalPay = totalPay + pw.calcPay();
                        list.add(pw);
                   
                        //adds to the employee counter
                        //totalEmployees = totalEmployees++;
                    
                        System.out.print(pw);
                        System.out.println("");
                        //System.out.print("Total Employees: " + totalEmployees);
                        break;
                    
                    case 2:
                        System.out.print("Annual Salary: ");
                        annualSalary = keyboard.nextDouble();
                        System.out.print("Annual Bonus: ");
                        annualBonus = keyboard.nextDouble();
                    
                        ShiftSupervisor ss = new ShiftSupervisor(employeeName, employeeID, shift, annualSalary, annualBonus);
                        totalPay = totalPay + ss.calcPay();
                        list.add(ss);
                        //adds to the employee counter
                        //totalEmployees = totalEmployees++;
                    
                        System.out.print(ss);
                        System.out.println("");
                        //System.out.print("Total Employees: " + totalEmployees);
                        break;
                    
                    case 3:
                        System.out.print("Hours Worked: ");
                        hrsWorked = keyboard.nextDouble();
                        System.out.print("Rate of Pay: ");
                        rOP = keyboard.nextDouble();
                        System.out.print("Monthly Bonus: ");
                        monthlyBonus = keyboard.nextDouble();
                    
                        TeamLeader tl = new TeamLeader(employeeName, employeeID, shift, hrsWorked, rOP, monthlyBonus);
                        totalPay = totalPay + tl.calcPay();
                        list.add(tl);
                   
                        System.out.print(tl + "\n"); 
                        break;
                }
            
                System.out.print("Enter new employee (Y/N)?: ");
                keyboard.nextLine();
                newEmployee = keyboard.nextLine();
                
            } while (newEmployee.matches(" [Yy] "));
        //((String.newEmployee).toUpperCase.charAt(0) == ('Y'));
        
        System.out.print("Total Employee Pay:" + " " + cf.format(totalPay));
        }
        
    }
    Thanks in advance!
    Last edited by Norm; 09-12-2011 at 12:53 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Need help with do while loop

    it doesn't read my input
    Print out the contents of the variable that holds your response immediately after it is read in to see what was read. System.out.println("newEmployee=" + newEmployee + "<")
    Compare that with what the while condition is looking for.

  3. #3
    smaster is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Need help with do while loop

    Thank you for editing my initial post.

    The program does read my input after I did that test, even though I enter 'Y' it still kick me out of the loop and prints out totalPay.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Need help with do while loop

    What is the value of condition in the while statement for the String "Y"?

    Do you understand how to code Regular expressions like you are using in the while?
    If not, perhaps you should either use a different method or work on getting a regex that works for what you want.

  5. #5
    smaster is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Need help with do while loop

    I got it working :) instead of using
    Java Code:
    while (newEmployee.matches(" [Yy] "));
    I used
    Java Code:
    while (newEmployee.equals("Y") || newEmployee.equals("y"));
    This seemed to do the trick, i'm not going to ask why.. my frustration has melted away.

    Thank you Norm for your quick response!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Need help with do while loop

    Or try: equalsIgnoreCase

  7. #7
    webdesignmk is offline Web design milton keynes
    Join Date
    Sep 2011
    Location
    2 Highley Grove, Broughton Leys, Milton Keynes, United Kingdom
    Posts
    1
    Rep Power
    0

    Default Re: Need help with do while loop

    Hey dude,
    Thanks for sharing dis...it seems to be very useful... :)

    Regards,
    [ commercial links removed - mod ]
    Last edited by JosAH; 09-12-2011 at 09:07 AM.

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  3. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM

Posting Permissions

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