14

I'm trying to convert a String variable into an integer, only the String looks like this (for example):

String string = " 12";

And so the String has a space in front of it. Now the String variable is being read from a .txt file which is why I'm having the issue of having the space in front of it. I need to convert the variable into an int, but when I try:

int integer = Integer.parseInt(string);

to pass the String into an integer, it compiles but I get an error when trying to run the program, since there is white space as part of the String.

Is there a way to pass a String of numbers with a space in front of it into an int? Would .trim work? And if so, how would I use that? (I'm not very familiar with .trim, I'm still learning!!) I appreciate the help! ^_^

keenns
  • 863
  • 4
  • 14
  • 26

6 Answers6

17

Would .trim work? And if so, how would I use that?

Yes, trim() will work, it will remove leading and trailing spaces from String,

int integer = Integer.parseInt(string.trim());
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • This is a really good answer. It has one drawback though. It's not Unicode-aware. See https://stackoverflow.com/questions/1437933/how-to-properly-trim-whitespaces-from-a-string-in-java and https://stackoverflow.com/questions/51266582/difference-between-string-trim-and-strip-methods-in-java-11 – SedJ601 Feb 04 '19 at 20:55
7

Use the string trim function in Java to first get a string without the spaces, then parse it...

1.) Remove spaces with trim ... String#trim

String stringWithoutSpace = string.trim();

2.) Parse the string without spaces ... Integer#parseInt

int integer = Integer.parseInt(stringWithoutSpace);

3.) Do everything above in one step ...

int integer = Integer.parseInt(string.trim());
Josh Engelsma
  • 2,636
  • 14
  • 17
6

The string split() method works well if you have a string of multiple numbers separated by spaces.

String numbers = "1 23 456";
String[] numbersArray = numbers.split(" "); // splitting string by spaces
System.out.println(numbersArray.toString());
// output ["1","23","456"]

Then you can use Integer.parseInt() on each index of the array. Hope that helps

2

If you using Java 11, you can use the String::strip method. For example:

int integer = Integer.parseInt(string.strip());

The strip() method is similar to trim() but uses Character.isWhitespace(int) to resolve spaces. (The Character.isWhitespace(int) method is Unicode-aware.)

Gilbert Lopez
  • 163
  • 1
  • 6
0

To make sure that your code does not throw NumberFormatException or even NullPointerException, when you get number from user, use apache common classes:

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
.....
// '1' is the default value which is returned when the string can not be converted to int
// The StringUtils.trimToEmpty do both left and right trim
int number = NumberUtils.toInt(StringUtils.trimToEmpty( "  12 ",1)) = 12; 
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
0

I think you should remove blank character before convert the string

int integer = Integer.parseInt(string.trim());

then, use parseInt or valueOf to convert the string for more information,please refer convert string to int

Asen
  • 1