how to stop While-Do Loop? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
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;

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

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);

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.

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.

Related

Do-while loop doesn't work for validating using a .equals method (Java) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
For an assignment, I had to make a program to see if the user input contained any of the three letters: r, c or p. If it was wrong, I had to keep asking the user to retype the input until the user typed the correct answer.[]
I tried using a do while loop to do the validation and I tested it myself, but instead of the invalid error message being returned only as long as the conditions were met, it returns it no matter what the user input was. The do statement ignored even the "valid inputs" and prints an invalid message regardless.[]
The main problem in your program is the condition you specified for the loop. If the input is not "c" or not "r" or not "p" the loop will continue. But the input will always be not "c" or not "r", if your input is for example "p". You should seperate the conditions with an and not an or.
while (!customerCode.contentEquals("r") && !customerCode.contentEquals("c") && !customerCode.contentEquals("p"));
You need to use a while loop here, not a do..while.
A do..while loop runs its body at least once, and then checks the condition - so it will show the error message at least once no matter what.
Instead, use a while loop, which checks the condition first:
while (input does not contain r, c or p) {
// Show error, ask for new input
}
// Input is now valid

Why isn't the text showing up? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am making a text based game, and my code works fine until this line when i type in "why" it doesn't print "test".
System.out.println( "Fallout: Master's dialogue");
System.out.println(" ");
System.out.println( " So, what shall it be? Do you join the Unity or do you die here? Join! Die! Join! Die! ");
System.out.print( "> ");
Go = keyboard.nextLine();
if (Go.equalsIgnoreCase("join"))
{
System.out.println("Excellent. Your talents will be useful. But first you must tell me everything about your vault.");}
System.out.print("> ");
Look = keyboard.nextLine();
if (Go.equalsIgnoreCase("why"))
{System.out.println("TEST");}
System.out.print("> ");
Look = keyboard.nextLine();
}
I assume Go and Look are string variables.
At the start of the code, you read the user input and stored it in Go:
Go = keyboard.nextLine();
And then you checked whether Go is join:
if (Go.equalsIgnoreCase("join"))
This is all fine and good.
Then, you read user input a second time, and stored it in Look:
Look = keyboard.nextLine();
But you incorrectly checked Go instead:
if (Go.equalsIgnoreCase("why"))
Go has not been changed. Go is still "join", so the condition is never true.
You should check Look instead:
if (Look.equalsIgnoreCase("why"))

Java: How to write a condition for operators? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
enter image description here
I need some help for an if condition. I know how the basic one works: if x == 1, return something. But how am I supposed to do it, when I need to define certain cases for different operators. Like if the operators is + then the result denoted the sum.
So basically I need to translate the condition in the link into base cases for a recrsive method. We learned that we always use if for basecases. I know how to that with smaller or bigger then, but with operators I don't know.
FYI: if() is not a loop while() is. if() statement works as true or false, if the statement is true then execute a certain code other wise some other code.
For example:
if(1==1){// yourcode } // Always as true
// or
String hello="hi there";
if(hello.contains("hi there")){ // Your code which if the statement happen to be true }
else { // Not true}
int x=3, s=1, i=2;
if(x==(s+i)){ // Your code which if the statement happen to be true }
else { // Not true}
also you can find a lot of tutorials online to help you better understand all the operators!
Are you trying to state if its positive or negative? If so you would do the following...
if(x >= 0){ //this operator is saying if x is greater than or equal to 0
// you can remove the equal sign to have it just greater or
// switch it to less than.
//if positive
}else{
//all other numbers, which would just be negative numbers
}

Why won't this scanner accept input on the println? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When my application compiles, it will not accept the input for roomNum on the same line in which it asks, "Please enter a room to search for:"
System.out.println();
if(roomNum < 0);
{
System.out.println("Please enter a room to search for: ");
roomNum = input.nextInt();
}
If I just use next instead of nextInt, it doesn't compile correctly.
The code above works, but will not accept the input on the same line which is the functionality I need.
Two things: Remove semi-colon after if condition and use System.out.print() instead if you want input on the same line.

What is wrong with my if-else syntax? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Currently new to java, and right now I'm doing an assignment involving if else statements. Can someone please why javac is reading my else as dangling? My syntax should be right unless I am missing something here.
You have to remove your Semicolon after the if-statement. Otherwise the braces after the if are not executed.
I hope this will fix your Problem.
if(Statement);
{
//The Content would not be executed in realtion to the if-statemen
}
Otherwise:
if(Statement)
{
//The Content would be executed in realtion to the if-statemen
}
remove the semicolon(;) after the if statement
You have a semicolon after your if. Remove it.
You have to remove the ; at the end of the line starting with if.
change :
if(...);
{
////
}
to
if(...)
{
////
}

Categories