This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

endless loop with nothing happening

 
Noah Faust
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Surrounded your source with the code tag)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint : you have a nasty semi-colon hidden somewhere.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Artur Lorincz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you are using the == operator for equality of reference types. You need to use the equals method.
 
Nishant Arora
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code tag to make your source more readable.
 
Lucas Franceschi
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic