• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get a chunk of a string and parse it into an integer  RSS feed

 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again

Man I love this place, anyways here is my problem of the day.

I created a combobox whose list is consisted of a string.

For example the Job Ids of people and their names in this form "12345 Johan Callabay".

I have been trying to extract the number as follows,


However, I get the error NumberFormatException.

How do I get that number out of the string

Thanks

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like you are doing the toString on the combo box itself, assuming that that is what instruct1Combo is.
You need to get the selected item out of it, which should be (in this case) your String.
There's a suitable method on https://docs.oracle.com/javase/8/docs/api/javax/swing/JComboBox.htmlJComboBox.
 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That looks like you are doing the toString on the combo box itself, assuming that that is what instruct1Combo is.
You need to get the selected item out of it, which should be (in this case) your String.
There's a suitable method on =https://docs.oracle.com/javase/8/docs/api/javax/swing/JComboBox.htmlJComboBox.


Argh I hate it when it turns out the solution is simple, Appreciate you, man. Cheers
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I mucked up the link...oh well!
Staff note (Knute Snortum):

The link in Dave's message has been corrected.

 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Looks like I mucked up the link...oh well!

No worries mate I still found my way in
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I would suggest a different approach altogether,
While your combobox can show strings, you may not always want to do that. In this case, you need it to show an Instructor who has a job id as well as name.



In the above example, you can easily get the selected item and work on it instead of doing String manipulation.
Ideally, I don't believe in providing a full baked solution, but here, I have made an exception for you since I feel its wrong to convert an Object to String and then do String manipulation in predicting the original object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!