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();
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 7 years ago.
Improve this question
I'm writing a little application that receives instructions in a StringBuilder filled with "(" and ")", for every ( the application sums 1, for every ( it decreases 1. I know it's a bit weird but it's a puzzle and it had to be done like that.
I'm having problems reading the content from the stringbuilder, Eclipse is complaining that it cannot access the specified index with charAt, what am I doing wrong?
I already checked and StringBuilder instructions is valid and properly filled with ( and ), no nulls or other symbols.
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==")"){
//do something
}
else(){
//do other thing
}
}
To use a Char in Java you use ' not "
so it should be :
public void walker(StringBuilder instructions){
for(int i=0; i<instructions.length();i++){
if(instructions.charAt(0)==')'){ //<-- note the change of " to '
//do something
}
else{
//do other thing
}
}
}
Edit 1:
The above solves the complication problem however
as #cricket noted, it should be charAt(i) for correctness of the code
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 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
this question might be simple for many of you but im stuck in it:/
this if condition works without while around it, but when i put while i get nothing in log
this REresponse is a response coming from server with a particular id that client sent to it plus a T or F as result to make sure the only client recieves it is the one who sent it
boolean recieved = false
String REresponse = (String) sInput.readObject();
while(recieved=false)
{
if (REresponse.equals("('T','"+newRID+"')")){
Log.i(LOGTAG,"you made it");
recieved = true;
}else if (REresponse.equals("('F','"+newRID+"')")){
Log.i(LOGTAG,"you failed");
recieved=true;
}else{
Log.i(LOGTAG,REresponse);
Log.i(LOGTAG,"some thing is wrong");
also i tried to use case condition but it does accpet this
the condition should be
while (received == false){
//...
}
if you write received = false you make an assignment, the boolean operator is ==
recieved = false. This always results in false so you never go into the loop when
You're assigning rather than equality in condition for while. You need to do as in follows.
while(received == false)
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
I need to display a error Message.I have declared a counter.Whenever the counter is 3 it has to display a different error message.So i have written the below snippet.
It returns error: <identifier> expected on the declaration.
//declaration
private static attempts = 0;
//operation
switch (xmlRpcFault.getFaultCode()) {
case 403:
attempts++;
if(attempts = 3)
{
mErrorMsgId = R.string.username_or_password_incorrect;
//reinitialize counter
attempts = 0;
}
else
mErrorMsgId = R.string.username_or_password_incorrectfull;
break;
The statement:
if(attempts = 3)
probably should be:
if (attempts == 3)
?
The source of the error you asked about is that you need to specify the actual type of the variable in your declaration. You have:
//declaration
private static attempts = 0;
You probably mean:
//declaration
private static int attempts = 0;
Note the int (or whatever type you want it to be).
Also, the issue that Harmlezz mentions (attempts = 3 vs. attempts == 3) is another problem.
As an aside: Most of the code you posted, as well as the question you asked, was irrelevant. The correct code to post would have been only the line with the compiler error. The correct question is "why does 'private static attempts = 0' cause a compiler error". Even the tags: This was not an Android question, or a question about while loops or if statements or counters. I mention this constructively: Think about the problem at hand, and try narrowing it down. It's a general thought process that will help you in all cases.
Granted, by posting all of it, it let us spot a few other issues, but that was incidental; your general problem solving thought-process did not match the problem itself.
Question titles are a good window into a person's thought process; in general, you can learn a lot by reexamining your question titles here after your problem is solved.
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.