Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to get information out of JTextField?

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh, I was thinking about that, thanks.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks.
 
Sheriff
Posts: 22575
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic