| Author |
endless loop with nothing happening
|
Noah Faust
Greenhorn
Joined: Mar 07, 2009
Posts: 26
|
|
my compiler OK'd this code but it doesn't seem to do anything. I think it may be a problem with the input. Here's the code:
|
public class Signature {
public static void main (String[] args) { System.print.ln (//insert witty phrase here) }
}
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
|
(Surrounded your source with the code tag)
|
[My Blog]
All roads lead to JavaRanch
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
|
Hint : you have a nasty semi-colon hidden somewhere.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
something to try... liberally sprinkle "System.out.println("got here 1");" type statements in there. I have gone so far as to make every other line a print statement when I dind't have a debugger. Each new line should have a different number.
Then, run your program. You can find EXACTLY where it gets stuck... you can see if you're in an endless loop, or if it dies somewhere, or if it's waiting for something. you can find out which branch of every 'if' statement you take, etc.
you can even use it to print out loop counters, such as your 'attempts' variable, to see if it is incrementing how you think it should.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Artur Lorincz
Greenhorn
Joined: Jun 30, 2003
Posts: 2
|
|
In the fourth line we declare the variable named "attempts" and initialize it with the value 0.
In the next line we have: which in combination with the previous line results in an endless loop.
The rest of the program will not get executed.
|
 |
Lucas Franceschi
Ranch Hand
Joined: Nov 10, 2008
Posts: 106
|
|
well, i dont want to give you the answer.
but this is really a typo, or lack of attention when typing.
when you type the line
you're using an semicolon that shouldn't be there, this will generate the infinite loop.
i'm sorry if I shouldn't give you the answer like that, but thats only lack of attention.
|
Lucas Franceschi
Software Developer for SGI Sistemas, lukas1596@gmail.com
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
We try to avoid straight answers, but I think in this instance, it might be better to let him out of his misery.
|
 |
Noah Faust
Greenhorn
Joined: Mar 07, 2009
Posts: 26
|
|
|
Thanks a ton for a straight answer! But I still have a problem, the program will either accept any entry for password, or none. This is mainly because I'm not sure what I'm doing in line 19 is correct.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
That is because you are using the == operator for equality of reference types. You need to use the equals method.
|
 |
Nishant Arora
Greenhorn
Joined: Apr 21, 2009
Posts: 16
|
|
import java.io.*;
public class Password {
public static void main (String[] args) {
int attempts = 0;
while (attempts < 3) {
System.out.println("Password");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String password = null;
try {
password = br.readLine();
} catch (IOException ioe) {
System.out.println("Error!");
System.exit(1);
}
if (password.equals("password")) {
System.out.println("Welcome user");
System.out.println(attempts + "failed attempts");
}
attempts = attempts + 1;
}
System.out.println("You don't know the password! GO AWAY!");
}
}
//don't use semicolon after while and replace == to string1.equals(string2) as you are comparing two strings.
|
 |
Noah Faust
Greenhorn
Joined: Mar 07, 2009
Posts: 26
|
|
Thanks, but my uncle helped me out with it. Turns out I needed the string comparison method. Here is the new code:
import java.io.*;
public class Password {
public static void main (String[] args) {
int attempts = 0;
while (attempts < 3) {
System.out.println("Password");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String password = null;
try {
password = br.readLine();
} catch (IOException ioe) {
System.out.println("Error!");
System.exit(1);
}
if (password.equals("password")) {
//Runtime.getRuntime().exec("cls");
System.out.println("Welcome user");
System.out.println(attempts + " failed attempts");
System.exit(1);
}else {
attempts = attempts + 1;
System.out.println("incorrect!");
}
}
System.out.println("You don't know the password! GO AWAY!");
}
}
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
|
Please use the code tag to make your source more readable.
|
 |
Lucas Franceschi
Ranch Hand
Joined: Nov 10, 2008
Posts: 106
|
|
well, i dont like to give the answer this way, but it would be evil to let the guy have all the suffering because of a typo.
hehe
I would help you with your problem, but it seems you already got an answer.
gracias.
|
 |
 |
|
|
subject: endless loop with nothing happening
|
|
|