943,695 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2757
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 4th, 2007
0

a for loop problem

Expand Post »
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 :

  
Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lamees is offline Offline
11 posts
since Oct 2006
Aug 4th, 2007
0

Re: a for loop problem

One thing I would not do is declare a variable inside of a loop:
Java Syntax (Toggle Plain Text)
  1. ...
  2. for (int i = 0; i < 5; i++) {
  3. int n = Integer.parseInt(t.getText());
  4. ...
Put this instead:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005
Aug 4th, 2007
0

Re: a for loop problem

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.
Java Syntax (Toggle Plain Text)
  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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Aug 4th, 2007
0

Re: a for loop problem

First of all, I'd like to thank you for your participation.Second, ...
Quote ...
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lamees is offline Offline
11 posts
since Oct 2006
Aug 4th, 2007
0

Re: a for loop problem

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lamees is offline Offline
11 posts
since Oct 2006
Aug 4th, 2007
0

Re: a for loop problem

Quote 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.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005
Aug 4th, 2007
0

Re: a for loop problem

Click to Expand / Collapse  Quote originally posted by lamees ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Aug 5th, 2007
0

Re: a for loop problem

Quote ...
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.

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

Quote ...
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 :

Quote ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lamees is offline Offline
11 posts
since Oct 2006
Aug 5th, 2007
0

Re: a for loop problem

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lamees is offline Offline
11 posts
since Oct 2006
Aug 5th, 2007
0

Re: a for loop problem

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:
    Java Syntax (Toggle Plain Text)
    1. button.addActionListener(new ActionListener() {
    2. public void actionPerformed(ActionEvent e) {
    3. // do stuff
    4. }
    5. });
What you've got is a good start.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: engg. java project
Next Thread in Java Forum Timeline: From Java beginner:Am I right with answers?:)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC