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
Okay so I need to ask the user to enter yes or no and then validate. I have to do this in a while loop. All of it compiles, I did not include the imports etc here. Here is what I have,
String str;
str = JOptionPane.showInputDialog("Enter answer, yes or no?");
while(!str.equals("yes")||!str.equals("no"))
{
str = JOptionPane.showInputDialog(" Invalid entry. Please enter yes or no.");
}
When I compile, it shows me the dialog to enter a string. If I enter yes it tells me invalid, if i enter no it tells me invalid. If I take the ||!str.equals("no") out it works fine but with only if I enter yes. So anyone can help me? Thanks..
For any non-null 'str'
while(!str.equals("yes")||!str.equals("no"))
will always be true.
Instead make it while(!str.equals("yes") && !str.equals("no"))
make it
while (!str.equals("yes") && !str.equals("no")) {
str = JOptionPane.showInputDialog(" Invalid entry. Please enter yes or no.");
}
Related
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 2 years ago.
Improve this question
as one of my first java projects, I want to build a mortgage calc.
I was trying to restrict the duration that the user can pick using this code
Scanner numOfPayments = new Scanner(System.in);
while (true) {
System.out.print("Enter number of payments (the number of Years you will be paying the loan):");
short numOfPayments2 = numOfPayments.nextShort();
if (numOfPayments2 >= 1 && numOfPayments3 <= 30) {
numOfPayments3 = (short) (numOfPayments2 * 12);
break;
}
System.out.println("Enter a valid number of years(Between 1 and 30)");
}
It worked with restricting other values, but not for this one.
Thanks in advance
Thanks for #azro for the answer
I should just change numOfPayments3 to numOfPayments2 inside the If statement
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 want to build a loop where the console keeps asking me for a string until I write the word "out". This is what I have till now, but it doesn't loop and I can't figure out why. Also I really want to use while or do while if possible.
Scanner input = new Scanner(System.in);
String name = input.nextLine();
while (!name.equals("out")) {
System.out.println(name);
break;
}
You're not updating name inside the loop and remove the break otherwise you're jumping out of the loop in the first iteration (which is not what you want)
while (!name.equals("out")) {
System.out.println(name);
name = input.nextLine(); // I've added it here for you
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I couldn't figure out why my code is printing forever. I tried a block (will show where I had it originally) but I want it to continue after printing the "help". My code goes as follows. I think it's the for loop... but I haven't touched much on it.
boolean yes = true;
while(yes) {
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
} else if (str1.equalsIgnoreCase("Help")) {
for(int g = 0; g < 2; g++) {
System.out.println(" Accepted commands:\n exit\n help\n load [filename]\n students\n search [partial name]\n assignments\n grades\n student [student name]\n assignment [assignment name]\n");
}
//I added the break here and it did print out once but I did not want it to end the program. With the break I did not need the for statement.
} else if (str1.equalsIgnoreCase("Load")) {
}
The value of str1 is never changed inside the loop. Perhaps you are missing some statement that takes new input from the user.
First off
boolean yes = true;
while(yes) {
You are never setting yes to false
Secondly
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
str1 is never set inside the loop, even to "Exit"
yes is set to true and you never changed it in the loop
read str1 inside the loop:
DataInputStream in = new DataInputStream(System.in);
str1=in.readLine();
while(true) {
str1=in.readLine();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
In this code, the user types in yes if they would like to play again
I have to use functions for this code, I cannot simply state while(again.equals("yes"));.
Part of code:
do{
System.out.print("Play Again? Yes/No: ");
String again=keyboard.nextLine();
boolean running=playAgain(again);
} while(running == true);
My question is why is }while(running==true); a syntax error? I declared the variable above it, shouldn't that allow it to run?
You need a "do" statement first, i.e. do { statements; } while (condition). Also
you don't need to save the return from playAgain() into a variable, you can call it directly from the while().
There are many Java tutorials out there, try the official Oracle ones from starters: The while and do-while Statements
Update
String again; // <== declared here because conditions inside
// while cannot see variables defined inside the do {} block
do {
System.out.print("Play Again? Yes/No: ");
again = keyboard.nextLine();
} while (playAgain(again));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
how can I ask the same question after I use break?
System.out.print("Employee Name: ");
sEmployee = input.nextLine();
boolean bInputOk = checkValue(sEmployee,0);
while(!bInputOk)
{
bInputOk = checkValue(sEmployee,0);
System.out.print("Enter a valid name");
break;
}
break breaks out of the loop. You want to only leave the loop if bInputOk == true. You already have that in your loop condition, and you can just drop the break. Also, you're going to want to actually prompt for input again, otherwise you'll just keep reusing the same invalid input (as Aduait Pokhriyal points out in question comments), e.g.:
while(!bInputOk) {
sEmployee = input.nextLine();
bInputOk = checkValue(sEmployee,0);
System.out.print("Enter a valid name");
}
By the way, you probably want to reverse the order of the print and the prompt there, otherwise your input and error messages will get a little confusing:
while(!bInputOk) {
System.out.print("Enter a valid name");
sEmployee = input.nextLine();
bInputOk = checkValue(sEmployee,0);
}
In general, your program will do exactly what you tell it to do. If you tell it to break out of the loop, it will do just that. The reason your original attempt behaved the way it did is because you told it to behave that way.