Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: Do while loop

  1. #1
    Jonesy29847 is offline Member
    Join Date
    Jul 2015
    Location
    Michigan
    Posts
    4
    Rep Power
    0

    Default Do while loop

    Hello,

    When I enter 99 I receive an array out of bounds error?

    Java Code:
    		while (empNo != 99){
    			System.out.print("Enter employee number for salary change (0-5): ");
    			empNo = input.nextInt();
    			System.out.printf("Enter salary change for %s, %s :", employee[empNo].getFirstName(), employee[empNo].getLastName());
    			double salarychange = employee[0].getSalaryChange();
    		
    			employee[empNo].setNewSalary(salarychange);
    			System.out.printf("New rate for employee: %s :",employee[empNo].showEmployeeData());
    		}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Do while loop

    Probably because, a) the employee array is defined to hold <= 99 elements (indexed from 0 to <= 98) and b) you do the check after you have already used the value. You need to check the value before you use it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Xen
    Xen is offline Member
    Join Date
    Jan 2015
    Posts
    86
    Rep Power
    0

    Default Re: Do while loop

    That is to say, you are using 99 as an escape code for exiting your loop.

    The simplest fix is:

    Java Code:
    while (true){
        System.out.print("Enter employee number for salary change (0-5): ");
        empNo = input.nextInt();
        if (empNo == 99) break;
        System.out.printf("Enter salary change for %s, %s :", employee[empNo].getFirstName(), employee[empNo].getLastName());
        double salarychange = employee[0].getSalaryChange();
     
        employee[empNo].setNewSalary(salarychange);
        System.out.printf("New rate for employee: %s :",employee[empNo].showEmployeeData());
    }
    OR

    Java Code:
    do {
        System.out.print("Enter employee number for salary change (0-5): ");
        empNo = input.nextInt();
        if (empNo >= 0 && empNo <= 5) {
            System.out.printf("Enter salary change for %s, %s :", employee[empNo].getFirstName(), employee[empNo].getLastName());
            double salarychange = employee[0].getSalaryChange(); 
            employee[empNo].setNewSalary(salarychange);
            System.out.printf("New rate for employee: %s :",employee[empNo].showEmployeeData());
        }
    } while (empNo != 99);
    This way your code will not produce errors (because the code is only executed when the index is between 0 and 5) and it will still only exit when you enter 99. Then you can expand this by saying:

    Java Code:
    do {
        System.out.print("Enter employee number for salary change (0-5): ");
        empNo = input.nextInt();
        if (empNo >= 0 && empNo <= 5) {
            System.out.printf("Enter salary change for %s, %s :", employee[empNo].getFirstName(), employee[empNo].getLastName());
            double salarychange = employee[0].getSalaryChange(); 
            employee[empNo].setNewSalary(salarychange);
            System.out.printf("New rate for employee: %s :",employee[empNo].showEmployeeData());
        } else {
            System.out.printf("%d is not a valid employee number", empNo);
        }
    } while (empNo != 99);
    Last edited by Xen; 07-28-2015 at 12:13 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Do while loop

    It is sort of a grey area but please don't spoon-feed solutions to posters. Otherwise they won't learn to do it themselves. It is okay to use code to demonstrate some concept or answer a question but for fixing their code it is better (and preferred) to talk them thru it.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 09:27 AM
  2. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 03:02 PM
  3. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  4. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  5. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 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
  •