aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes for loop is not updating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "for loop is not updating " Watch "for loop is not updating " New topic
Author

for loop is not updating

Youssef Eid
Greenhorn

Joined: Dec 20, 2013
Posts: 4
My for loop is not updating, I'm getting the output "mmmmmm" or "ffffff" of the same letter the user inputs each time. I want it to constantly update the next letter each time like this:

user input: m
user input: f
user input: d

output: "Letters Used: mfd"

Rajat K Gupta
Greenhorn

Joined: Dec 14, 2013
Posts: 13
Can you please tell how you are getting input?
Unable to understand

Please provide more information on tf.
Youssef Eid
Greenhorn

Joined: Dec 20, 2013
Posts: 4
I'm getting input through JTextField. I Declared it as a static so I could use it throughout my code.

Rajat K Gupta
Greenhorn

Joined: Dec 14, 2013
Posts: 13
See what you are doing in the loop:-
1. You are iterating over the array but everytime you increment the value of "j", tf remains same.

It means in all the elements of the array you are storing the same value and hence you are getting the correct corresponding to the values stored in elements.
Youssef Eid
Greenhorn

Joined: Dec 20, 2013
Posts: 4
How can I fix it?
Rajat K Gupta
Greenhorn

Joined: Dec 14, 2013
Posts: 13
The way to fix is: you need some way in which you can get from JTextField iteratively. It means you need a way in which you can iterate simultaneously on TextField and the String array so that you can store the proper values in the array.

Try you logic!!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Youssef Eid wrote:I'm getting the output "mmmmmm" or "ffffff"

No surprises there. The value of the textfield doesnt change between loop iterations.
You need to:
1) Add an action listener to the text field
2) In action performed get the value and store in the array
3) Once done obtaining 3 values, print them

Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

er.rajat gupta wrote:The way to fix is: you need some way in which you can get from JTextField iteratively. It means you need a way in which you can iterate simultaneously on TextField and the String array so that you can store the proper values in the array.
Try you logic!!

Thats not the correct thing to do. Read my reply above
 
 
subject: for loop is not updating