• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get information out of JTextField?

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently making a program where if you put multiple values inside a JTextField (Obviously separated by commas), that it will take the values of that and print it out in numeric order. This isn't the whole program, just part of it.
I can set a String equal to the getText() of the JTextField, and then parseInt that to an actual number. The only problem is I don't understand how to make it work for multiple values? I can't set a String[] equal to the getText() of the JTextField. I can't make an int[] parseInt the String equal ot the getText() of the JTextField.
If someone could help that would be great.


Only works if I want to put a single value into the JTextField, which is what I don't want.


Tried this, but it doesn't work.


 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to have to get the String from the JTextField and split it up into multiple values. Coincidently, String has a method called split(). Take a look at that.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, I was thinking about that, thanks.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, haven't really tested it yet, I came up with the second error part. Even if I split it, how do I do parseInt to all of the strings that are typed?
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One at a time. String.split() returns a String array. Use a do loop.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thanks.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also think about error handling. What if some part of the String isn't a number of comma? Also, before parsing, you may want to consider trimming the Strings as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!