a for loop problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 11
Reputation: lamees is an unknown quantity at this point 
Solved Threads: 0
lamees lamees is offline Offline
Newbie Poster

a for loop problem

 
0
  #1
Aug 4th, 2007
Hi, everybody. I've made a code of a "for" loop. Once i exceeds 4, the loop must stop. The input is in a JTextField. The problem is that the loop never stops although it exceeds 4. Can anybody help me?

The problem is here :

  
  1.  
  2. public void compare(int x)
  3. {
  4.  
  5. for(int i=0;i<5;i++)
  6. {
  7.  
  8. int n=Integer.parseInt(t.getText());
  9.  
  10. if(n==x)
  11. {
  12. l2.setText("Astonishing ! The number is really "+n);
  13. t.setEditable(false);
  14. break;
  15. }
  16. else if(n>x)
  17. {
  18.  
  19. l2.setText("Move towards less values");
  20. continue;
  21. }
  22. else if(n<x)
  23. {
  24. l2.setText("Move larger");
  25. continue;
  26. }
  27.  
  28. }
  29. t.setText(null);
  30.  
  31. }

For more details please contact me through the site. Thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: a for loop problem

 
0
  #2
Aug 4th, 2007
One thing I would not do is declare a variable inside of a loop:
  1. ...
  2. for (int i = 0; i < 5; i++) {
  3. int n = Integer.parseInt(t.getText());
  4. ...
Put this instead:
  1. public void compare(int x) {
  2. int n = Integer.parseInt(t.getText());
  3. for (int i = 0; i < 5; i++) {
  4. ...
  5. ...
  6. }
  7. }
This will cause the program to needlessly re-parse that value in the JTextField. That's just a style pointer though, and doesn't get to the source of the problem.

I'm almost certain the problem lies with those continue; statements. What continue does, is halts the loop at the current step, and forcefully start it from the beginning again.

Take out the continue statements, and see if that changes anything; the loop will continue after any instruction in the if/else/else-if blocks are executed.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: a for loop problem

 
0
  #3
Aug 4th, 2007
I assume you are wanting the user to get 5 guesses at the number before ending. This isn't really what your loop is doing though. It is evaluating the same input 5 times (or less if they got it right) and then exiting.
  1. int n = Integer.parseInt(t.getText());
will not wait for input from the user line a Scanner.nextInt() would. It just reads that field each iteration and keeps going. You will need to move the loop up to a different method (like a "Try Again" button handler) that allows them to enter up to 5 guesses before begin disabled. You could leave the the compare code in your method if you like, but you will need the loop to be separate so that you only read new input when they hit the button (or press enter if you want to use an ActionListener on the test field).
Last edited by Ezzaral; Aug 4th, 2007 at 3:00 pm. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 11
Reputation: lamees is an unknown quantity at this point 
Solved Threads: 0
lamees lamees is offline Offline
Newbie Poster

Re: a for loop problem

 
0
  #4
Aug 4th, 2007
First of all, I'd like to thank you for your participation.Second, ...
This will cause the program to needlessly re-parse that value in the JTextField.
I need to make this as the user'll enter different numbers and the program should compare each number to a randomly generated one. My program does the comparison well, but it doesn't stop after the 5 iterations are completed. I think you are right concerning the "continue" statements. However, it didn't make any difference when I removed them!
I can't teach anybody anything, I only can tell them how to think (Socrattes).
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 11
Reputation: lamees is an unknown quantity at this point 
Solved Threads: 0
lamees lamees is offline Offline
Newbie Poster

Re: a for loop problem

 
0
  #5
Aug 4th, 2007
Thanks for replying to my question. Adding a "try again" button is a good idea, but I'm restricted to a certain design consisting of : one text field, two labels and only one button. Thanks again!
I can't teach anybody anything, I only can tell them how to think (Socrattes).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: a for loop problem

 
0
  #6
Aug 4th, 2007
Originally Posted by lamees
I need to make this as the user'll enter different numbers and the program should compare each number to a randomly generated one.
That's fine, but having the loop repeatedly call Integer.parseInt() is just going to re-parse the first number it comes across (if they're separated by spaces) or it will just mush all of the integral values together to make one big number.

Please stop me if I'm on the wrong track, but shouldn't the user have to click a button to get it to compare the value they typed in and the number? Every time the user enters a new number, they will have to click a "Compare" button of sorts.

Why does it make 5 iterations?

Could you please describe what exactly you're trying to get your program to do?

EDIT:
Not going to lie, this is starting to sound a little like homework. But don't get me wrong, I have nothing against helping people with homework, I just won't do it for them. I have also found it's much easier to help somebody when they post the assignment verbatim.
Last edited by indienick; Aug 4th, 2007 at 4:29 pm.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: a for loop problem

 
0
  #7
Aug 4th, 2007
Originally Posted by lamees View Post
Thanks for replying to my question. Adding a "try again" button is a good idea, but I'm restricted to a certain design consisting of : one text field, two labels and only one button. Thanks again!
That's fine. Keep a counter of the number of tries, increment when user hits your button to compare a guess, and when they hit five tries it's over. You really don't even need a loop for anything at all.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 11
Reputation: lamees is an unknown quantity at this point 
Solved Threads: 0
lamees lamees is offline Offline
Newbie Poster

Re: a for loop problem

 
0
  #8
Aug 5th, 2007
Please stop me if I'm on the wrong track, but shouldn't the user have to click a button to get it to compare the value they typed in and the number? Every time the user enters a new number, they will have to click a "Compare" button of sorts.
The comparison should be made automatically by pressing "enter", not through a button as the problem says.

Why does it make 5 iterations?
To grant the user the chance to try guessing the number more than one time.

Could you please describe what exactly you're trying to get your program to do?
*The program is supposed to build a GUI interface of two labels, one text field ,and one button.
*The program should generate a random number from 1 to 1000.
*The user role is to guess the random number through a fixed number of trials (which is 5 here).
*One of the labels is for displaying a certain statement that shouldn't be changed. The other has three states :
1- when the guessed number = the random number.
2- when the guessed number > the random number.
3- when the guessed number < the random number.
*At each state the label changes the statement after every trial.
*At state "1" the text editor becomes uneditable.
*The background color should be changed to "red" when the user get near the random number and changed to "blue" when the user get far from the random number.

CONCERNING THE EDIT PART OF YOUR REPLY :

EDIT:
Not going to lie, this is starting to sound a little like homework. But don't get me wrong, I have nothing against helping people with homework, I just won't do it for them. I have also found it's much easier to help somebody when they post the assignment verbatim.
You are right this is a homework, but I don't like to make somebody to do my work. First I keep trying until I reach. Once I find a problem that I can not handle, I try to get help only with the parts I couldn't solve. I've explained the whole task to make you get the point, but I'm able to solve it except the part I asked about.

For more details :
I've uploaded the code I made on a site by which it can be viewed without downloading and this is the link :
http://rafb.net/p/gqMbip25.html
I can't teach anybody anything, I only can tell them how to think (Socrattes).
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 11
Reputation: lamees is an unknown quantity at this point 
Solved Threads: 0
lamees lamees is offline Offline
Newbie Poster

Re: a for loop problem

 
0
  #9
Aug 5th, 2007
Sorry, forgot to say that the button is made to start a new game, i mean to generate a new random number and follow the steps from the beginning.
I can't teach anybody anything, I only can tell them how to think (Socrattes).
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: a for loop problem

 
0
  #10
Aug 5th, 2007
Sweet.

Alright. I looked over the code you have, and I would do it much differently than you have done it (while still staying within the requirements).
  1. Get rid of the constructor for the class. Move the contents into the main() function.
  2. I wouldn't implement java.awt.event.ActionListener. Instead, I would use an inline class like so:
    1. button.addActionListener(new ActionListener() {
    2. public void actionPerformed(ActionEvent e) {
    3. // do stuff
    4. }
    5. });
What you've got is a good start.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2307 | Replies: 14
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC