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));
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 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 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 8 years ago.
Improve this question
I ran into this problem. This is my code:
String answer;
System.out.println("Choose a day");
answer = tastatur.nextLine();
if(svar.equals("saturday"))
System.out.println("Saturday");
I want to use the answer in the if statement.
You are looking at the wrong variable.
if( answer.equals("saturday") ){
System.out.println("Saturday")
}
What's the logic?
You are reading what the user is entering into the answer variable. In your code you are checking the svar variable while you should be checking the answer variable. If indeed the user entered Saturday, you will print it on the screen. Else, not.
equalsIgnoreCase() is a better method to use because Saturday and saturday are different in the sense that one has a capitalized S and the other does not. equals() will treat them as different.
You have said that Java is new to you. I hope you are initializing the tastatur variable properly as:
BufferedReader tastatur = new BufferedReader(new InputStreamReader(System.in));
and using the import java.io.* in your program.
As an aside, please read the official tutorials provided by Java. They are a good resource. Ask questions on StackOverflow when you have run out of luck while looking for answers yourself. With that said, welcome to StackOverflow where we very mercilessly close questions if they do not meet our standards or contain spam.
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.
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 9 years ago.
Improve this question
is there a way to tell the compiler in Java or Android to not remove some statements in code -which are intended to clean up variables after use to prevent any data remnant in ram-??
would creating a dummy method solve this issue??
these statements basically set the variables to their type-based initial values..
Thanks in advance!
The code that you describe is not dead code.
Dead code is code that will never execute.
Here is an example:
private int secretSchmarr;
public boolean blammo()
{
boolean returnValue;
secretSchmarr = calculateSecretValue();
returnValue = useSecretValue(secretSchmarr);
secretSchmarr = 99; // this is not dead code.
return returnValue;
secretSchmarr = 98; // This is dead code because it can never execute.
}
I answer under the odd assumption that you have a good reason to believe that the code is still useful even though it is dead.
Store the value false in some obfuscated form that the compiler can't understand. Then, conditionally branch to that code using your obfuscated value. The compiler will not know it is dead, so it will not be removed.
I'll use a file for my example, but it is probably not the most efficient way. Say your code that the compiler thinks is dead code was in a function called myCode(). Assume that fin is reading from a file that only contains false followed by EOF
if(Boolean.parseBoolean(fin.next()))
myCode();