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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I found a few weird constructs in Java that the compiler allows, but for which I'm not sure what could be the practical use.
1) if statement:
if((score=score+10) > 110); //No if body
while eg: switch(++i); is not
2) for loop:
for(;;); //No loop body
Are there practical, valid circumstances to use the preceding code?
This:
if((score=score+10) > 110);
is equivalent to:
score += 10;
but has no practical use otherwise.
This:
for(;;);
loops forever doing nothing - not particularly useful, unless you wanted to create a permanently busy thread perhaps for testing purposes.
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 7 years ago.
Improve this question
I am using Java and have some problems.
I made two BigInteger variables, p and q.
In my code, I want to add if function, like if(p=1 \ q=1).
I tried many ways but there was error. Do you know how to solve this?
Your question is not completely clear, but you need to use the BigInteger.equals() method, as in this example:
if (BigInteger.equals(p, BigInteger.ONE) || BigInteger.equals(q, BigInteger.ONE)) {
// do something
}
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 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 am developing a Java program and I'm meeting cases where I get undecided whether to use the casting a string to integer method, or to use the integer.parseInt method. Is there any clear benefit for either of the two methods?
With 'casting to string method', I mean:
String.valueOf(integer);
As far as I know, it's not possible to cast from a String to an int, so using Integer.parseInt seems like the best option here.
Looking at your edits about using valueOf, perhaps this link may help: Integer.valueOf() vs. Integer.parseInt()
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Hi I want to print the SingleLinkedList in reverse.
static void Collections.reverse(List list):
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html
See Collections.reverse (JavaDoc)
I don't know for "The SingleLinkedList" class (is it even a List implementation ?) but if it works as its name states, the best way (more effective one) would be to go through the data structure and put every element in a stack. Then simply read the stack.
public print(Element el) {
if (el.next!=null)
print(el.next);
System.out.print(el.value+" ");
}
Basicaly its puting it in (call) stack an reading it (as someone else sugested). Nonrecursive version using explicit stack is usualy faster, does not risk StackOverflowException and takes longer to write.