This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Converting String to Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting String to Integer" Watch "Converting String to Integer" New topic

Converting String to Integer

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hello everyone,

I'm having a little bit of trouble converting a string a an integer:

If I set string1 = "5" and string2 = "4" the output is "9". But like I excepted it doesn't work when the strings have spaces in them . How do I remove does spaces in the beginning? Does anyone know a built-in method from the APIs or an alternative way around this problem


[edit - change 'quote' tags to 'code' tags]
[ February 29, 2008: Message edited by: fred rosenberger ]

Joanne Neal

Joined: Aug 05, 2005
Posts: 3169

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 10925


I change your post a little. When you want to post code, don't use the 'quote' tags, but the 'code' tags. This will preserve your formatting, making it easier to read.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Oh oops. My bad. Thanks for fixing it
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36579
Go through the Integer class; there are probably several methods and a constructor which take Strings as parameters.
I agree. Here's the link:
subject: Converting String to Integer
Similar Threads
Lost in simple applets
just basic division
Command line arguments
System.out.println confusion with the quotation marks and + sign
Doubt on PriorityQueue?