| | |
a for loop problem
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 11
Reputation:
Solved Threads: 0
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 :
For more details please contact me through the site. Thanks a lot.
The problem is here :
Java Syntax (Toggle Plain Text) -
- public void compare(int x)
- {
-
- for(int i=0;i<5;i++)
- {
-
- int n=Integer.parseInt(t.getText());
-
- if(n==x)
- {
- l2.setText("Astonishing ! The number is really "+n);
- t.setEditable(false);
- break;
- }
- else if(n>x)
- {
-
- l2.setText("Move towards less values");
- continue;
- }
- else if(n<x)
- {
- l2.setText("Move larger");
- continue;
- }
-
- }
- t.setText(null);
-
- }
For more details please contact me through the site. Thanks a lot.
One thing I would not do is declare a variable inside of a loop:
Put this instead:
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
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.
Java Syntax (Toggle Plain Text)
... for (int i = 0; i < 5; i++) { int n = Integer.parseInt(t.getText()); ...
Java Syntax (Toggle Plain Text)
public void compare(int x) { int n = Integer.parseInt(t.getText()); for (int i = 0; i < 5; i++) { ... ... } }
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.
-Ginsburg
Don't tell me to "google it" - I already have.
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. 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).
Java Syntax (Toggle Plain Text)
int n = Integer.parseInt(t.getText());
Last edited by Ezzaral; Aug 4th, 2007 at 3:00 pm. Reason: typo
•
•
Join Date: Oct 2006
Posts: 11
Reputation:
Solved Threads: 0
First of all, I'd like to thank you for your participation.Second, ...
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!
•
•
•
•
This will cause the program to needlessly re-parse that value in the JTextField.
I can't teach anybody anything, I only can tell them how to think (Socrattes).
•
•
•
•
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.
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.
-Ginsburg
Don't tell me to "google it" - I already have.
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.
•
•
Join Date: Oct 2006
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
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?
*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.
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).
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).

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).
- Get rid of the constructor for the class. Move the contents into the main() function.
- I wouldn't implement java.awt.event.ActionListener. Instead, I would use an inline class like so: Java Syntax (Toggle Plain Text)
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- // do stuff
- }
- });
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.
-Ginsburg
Don't tell me to "google it" - I already have.
![]() |
Similar Threads
- nested for loop problem (Java)
- Alright this loop problem is bugging me (Java)
- I think Loop problem (Java)
- Cold Fusion Loop problem (ColdFusion)
- Help w/ for loop. (C++)
- loop problem (C)
- Random Different Number in each loop ?? HLP Me PLZ (c or c++) (C++)
- loop problem (C++)
Other Threads in the Java Forum
- Previous Thread: engg. java project
- Next Thread: From Java beginner:Am I right with answers?:)
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






