Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop is not updating

 
Youssef Eid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell how you are getting input?
Unable to understand

Please provide more information on tf.
 
Youssef Eid
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting input through JTextField. I Declared it as a static so I could use it throughout my code.

 
Rajat K Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I fix it?
 
Rajat K Gupta
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic