-2

the System.out.print("\n\n\t\t Do you want to see the next record ? [y/n]"); keeps repeating and repeating how can i stop it when the Records becomes 0.

case 3: //Previous
    if (recno!=0) {
        String pre;

        System.out.print("\n\n\t\t\t\t   Previous Record");
        System.out.print("\n\n\t\t\t    Employee Number: EMP-"+EmpNo[recno]);
        System.out.print("\n\t\t\t    Employee Name: "+EmpName[recno]);
        System.out.print("\n\t\t\t    Salary: "+Salary[recno]);
        System.out.print("\n\t_________________________________________________________________");

        do {
            System.out.print("\n\n\t\t  Do you want to see the next record ? [y/n]");
            pre = reader.readLine();

            if(pre.equals("y")) {
                recno--;
                System.out.print("\n\n\t\t\t    Employee Number: EMP-"+EmpNo[recno]);
                System.out.print("\n\t\t\t    Employee Name: "+EmpName[recno]);
                System.out.print("\n\t\t\t    Salary: "+Salary[recno]);
            }

            menu = display.charAt(0);
            System.out.print("\n\t_________________________________________________________________");
        } while(menu=='n');

        System.out.println("Thank You for Using this Program!");
    }
    else {
        System.out.print("\n\n\t\t\t\tRecord Not Found!");
    }

break;
admdrew
  • 3,790
  • 4
  • 27
  • 39
  • 1
    please prettify your code. it's indented too much to the right. thnx – Caffeinated Aug 07 '14 at 16:20
  • possible duplicate of [Breaking out of nested loops in Java](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java) – rajah9 Aug 07 '14 at 16:22
  • You probably should use a `while` in a do-while loop – jhobbie Aug 07 '14 at 16:22
  • 1
    @jhobbie There's a `while` there, it's just way over to the right where it's hard to find. It took me a ... ummm, *while* to find it too. – ajb Aug 07 '14 at 16:24
  • You need to show more code snippet like What is `menu` declared as variable? and what is `display`? – Smit Aug 07 '14 at 16:28

4 Answers4

1

To stop the loop use if (condition) break; with the proper condition; put this code in the proper place inside the loop.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
1

You use the key-word "break".

In any loop, whether it is a while, do-while, or for-loop, you can always break the loop with the keyword as a statement "break".

So like this:

do {
     if(condition) {
          break;
     }
while(condition);
0

I think you may be looking for a break statement.

This question shows the break in action. The accepted answer shows how to break out of two loops.

Community
  • 1
  • 1
rajah9
  • 11,645
  • 5
  • 44
  • 57
0

In your code, the do-while loop condition is determined by the first character of display being "n". In your loop, display is never updated, so the condition that "n" is the first letter of display is never met. You can also choose to change your logic as the other answers have suggested by using the break statement.

Tyler Ritchie
  • 170
  • 1
  • 9