0

I'm getting confused with this do while statement. I want to make if the user input y then it will loop back to do loop. I'm not sure how to command back to do loop since C++ you can use gotokeyword if I'm not mistaken.

do
    {
    System.out.print( "\nPlease Make A Choice :");
    input = stdin.readLine();
    x = Integer.parseInt(input);

    if (x == 1)     
        CalculateCircleArea.GetRadius();    
    else if (x == 2)
        CalculateRectangleArea.GetLengthAndHeight();
    else if (x == 3) 
        CalculateTriangleArea.GetHeightBaseAndBaseLength();
    else

    System.out.print("\t      WRONG INPUT");
    String input2;
    String abc = "\n\tDO YOU WANT TO CONTINUE?";
    String abc2 = "\n\t   PLEASE CHOOSE (Y/N):";
    String abc3 = abc.concat(abc2);
    System.out.print(abc3);
    input2 = stdin.readLine();  
    }while (choice == 'y');
Edie
  • 65
  • 1
  • 2
  • 11

5 Answers5

3

You can use the continue statement to start a new iteration of the loop.

do{
    ...
    continue;    // Stops the current loop and continues to the next iteration.
    ...
}while(...);

Also, I can offer some improvements to the code.

Use this

x = stdin.nextInt();

Instead of

input = stdin.nextLine();
x = Integer.parseInt(input);

Scanner.nextInt() will return the next integer it finds and is more efficient than reading in a String and converting it to an int. Using your current configuration is useful, however, when checking for input errors by using a try-catch block.

Use

System.out.print("\n\tDO YOU WANT TO CONTINUE? \n\t    PLEASE CHOOSE (Y/N)");

Instead of

String abc = "\n\tDO YOU WANT TO CONTINUE?";
String abc2 = "\n\t   PLEASE CHOOSE (Y/N):";
String abc3 = abc.concat(abc2);
System.out.print(abc3);

The former just makes sense and is more efficient, as you don't have to concatenate the Strings etc.

Also, choice doesn't seem to be declared or used anywhere. Are you sure that you aren't supposed to be using input2? If so, use the String.equalsIgnoreCase("") method instead of ==, as == compares the object reference and not value.

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
2

Should use like:

while(choice.equalsIgnoreCase("y"))

instead of == this is not used to compare strings.

Also I don't see choice being set you may want input2 instead of choice?

while(input2.equalsIgnoreCase("y")
brso05
  • 13,142
  • 2
  • 21
  • 40
  • @brso05 It's works... Thank you very much. You help me a lot sir.. :) I will make your answer as my reference.. thanks again – Edie Oct 16 '14 at 20:22
1

I think you should read the user input into the right variable :

//...
  input2 = stdin.readLine();  
}while (choice == 'y');

Replaced by

//...
  choice = stdin.readLine();  
}while ("y".equals(choice));
0

Shouldn't the condition be like this?

    input2 = stdin.readLine();  
}
while (input2.equals("y"));
gab06
  • 578
  • 6
  • 23
0

Used choice as String just to make your solution easy.

do{
    //.. 
    }while (choice == "y");

this code will be right if choice is interned otherwise its good to use equals() method to compare string.

do{
    //...
    input2 = stdin.readLine();  
}
while (input2.equals("y"));

For more details

Community
  • 1
  • 1
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40