1

Possible Duplicate:
How to convert string to int in Java?

MY code is supposed to read strings and then take the corresponding actions, but if that string is a line of numbers i need this line as a full number (an int) not as a string anymore.can this be done?

Community
  • 1
  • 1

3 Answers3

7

Use Integer.valueOf:

int i = Integer.valueOf(someString);

(There are other options as well.)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • yup,that will do. Thanks – Angelo Mico Jan 05 '13 at 23:30
  • 2
    This will work, but has unnecessary boxing and unboxing. Better to use `Integer.parseInt(...)` for primitive `int`. – clstrfsck Jan 05 '13 at 23:48
  • yep. As per the documentation, valueOf returns an **Integer Object** whereas parseInt returns a **primitive int** and that's what you want. (http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt%28java.lang.String%29) – wullxz Jan 06 '13 at 00:03
  • It wasn't clear to me since the title said "integer" (but lower-case) and the text said "int". I find it unlikely the boxing is going to cause the OP timing issues. – Dave Newton Jan 06 '13 at 00:05
  • It might not be a problem for the OP but it might be a problem for a future answer seeking coder. It should at least be mentioned when proposing the `valueOf` method. – wullxz Jan 06 '13 at 00:07
  • @wullxz It's mentioned explicitly in the linked documentation; IMO that's enough--feel free to edit the answer. – Dave Newton Jan 06 '13 at 00:08
  • I will try both valueOf and Integer.parseInt. anyway thanks everyone – Angelo Mico Jan 06 '13 at 01:37
  • @AngeloMico They'll both work, they do the same thing (in their simplest form)--the comments regarding the autoboxing are correct; if you need your code to be as fast as possible, or truly need an `int`, `parseInt` is a better choice. I tend to stick to objects because I don't do a lot of numerically-intensive work in Java. – Dave Newton Jan 06 '13 at 01:38
  • nah i don't really care to make my code faster,just need something that works,I'm new to java so my supervisor doesn't want anything else other than a correct code – Angelo Mico Jan 06 '13 at 01:48
3

Look at the static method Integer.parseInt(String string). This method is overloaded and is also capable of reading values in other numeral systems than the decimal system. If stringcan't be parsed as Integer, the method throws a NumberFormatException which can be catched as follows:

string = "1234"
try {
   int i = Integer.parseInt(string);
} catch (NumberFormatException e) {
   System.err.println(string + " is not a number!");
}
wullxz
  • 17,830
  • 8
  • 32
  • 51
2

In addition to what Dave and wullxz said, you could also user regular expressions to find out if tested string matches your format e.g.

import java.util.regex.Pattern;
...

String value = "23423423";

if(Pattern.matches("^\\d+$", value)) {
   return Integer.valueOf(value);
}

Using regular expression you could also recover other type of numbers like doubles e.g.

String value = "23423423.33";
if(Pattern.matches("^\\d+$", value)) {
    System.out.println(Integer.valueOf(value));
}
else if(Pattern.matches("^\\d+\\.\\d+$", value)) {
    System.out.println(Double.valueOf(value));
}

I hope that will help to solve your problem.

EDIT

Also, as suggested by wullxz, you could use Integer.parseInt(String) instead of Integer.valueOf(String). parseInt returns int whereas valueOf returns Integer instance. From performance point of view parseInt is recommended.

Tom
  • 26,212
  • 21
  • 100
  • 111
  • 1
    I'd suggest you to change your use of `valueOf` to `parseInt` respectively `parseDouble`. (see the comments under **Dave**'s answer) – wullxz Jan 06 '13 at 00:05
  • @wullxz very good; from performance point of view that method is recommended (+1). – Tom Jan 06 '13 at 00:13