• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

endless loop with nothing happening

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Surrounded your source with the code tag)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint : you have a nasty semi-colon hidden somewhere.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 106
Mac Mac OS X Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic