Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Adding Strings

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I add the values of numbers in a String together? For example, I want to get String "1" and String "2" to be added together to make another string with the value of 3. How?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the Integer.parseInt() method to obtain the integer values of the String objects and then add them together.
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to gather input from several JTextFields, convert them into ints, then add them together, and turn the resulting int into a string so that I can display it in another JTextField. The problem with that is that if there is an empty space in the JTextField, ParseInt displays an error, and the program crashes. So, I was thinking of skipping the int conversion part.
 
Marshal
Posts: 76483
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you have to convert it to an int. No two ways about it. You can use the String#trim() method to remove some whitespace, but you would do better to verify that the text is in the correct format; if they write 123 456 that is invalid and the user must enter a new number.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alix Ollivier wrote:I am trying to gather input from several JTextFields, convert them into ints, then add them together, and turn the resulting int into a string so that I can display it in another JTextField. The problem with that is that if there is an empty space in the JTextField, ParseInt displays an error, and the program crashes. So, I was thinking of skipping the int conversion part.



You must use Integer.parseInt() on JTextField(s) content in order to do what you want.
Note that parseInt throws NumberFormatException if the input is not valid (the given string cannot be parsed into integer value), so you can use it to validate user's input before doing what you want to do with those integer values.
 
I'm doing laundry! Look how clean this tiny ad is:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic