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 a complete beginner at Java, and I have encountered a problem with the While loops. I just started them, so to practice I copied the example code into Eclipse and tried to run it, but there is no output (code below).
I tried debugging it, so I searched for instructions and tutorials online. I get to the part where I am in the debugging view, and when I'm trying to run the While loop, nothing happens. I click the button to Step into, as all the instruction say, and there should be some output shown there, but there just isn't. Can someone help me with that? How can I get an output from that code? I tried it in Eclipse and TextPad text editor. The program complies okay, but when I run it there is no output.
Debugging view
After clicking step into
Code Picture
Code:
public static void main(String[] args) {
int value = 1;
while (value<10);
{
System.out.println("Hello!");
value=value+1;
}
}
Your while doesn't loops because you've inserted a semicolon ; after the while loop declaration.
You have to remove it to make your loop able to enter in its scope, like this:
public static void main(String[] args) {
int value = 1;
while (value<10) {
System.out.println("Hello!");
value=value+1;
}
}
You need to remove the ; by while (value<10) than it should work
Related
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need to print Swedish words that contain unicode using Java
how can I do it?
for example
Text with Unicode:- \u228sk\u228da
Output: åskåda
Both of the following would work:
class Main {
public static void main(String args[]) {
System.out.println("åskåda");
System.out.println("\u00e5sk\u00e5da");
}
}
Note that you need to use the hex values and must take care of specifying the encoding when reading data (from disk or network)
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 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 compiled the following code in java and it compiled!
How?
And when i run it it just runs without any output!
Why did it compile?
public class Check{
public static void main(String args[])
{
for(int i=0;i!=0;i++)
System.out.print(i);
}
}
Indeed the program "does nothing" as far as the user can see. But the instructions(code) you have given to the compiler does in fact have valid statements in it that are syntactically correct. If there are semantic errors in your program, that is up to us as programmers to sort out. This may be considered a bug - but it is one that us, the coders must find and fix - not the compiler.
Hovercraft full of eels' comment on your post explains nicely why there is no output.
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 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();