-4

I get: java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

when I try to use:

Integer.parseInt("h");

or

Integer.parseInt("h".toString());

but I was expecting it to work like atoi in c.

What am I doing wrong here?

Beska
  • 12,445
  • 14
  • 77
  • 112
menemenemu
  • 209
  • 1
  • 2
  • 8
  • 6
    And what you expect to receive? – Andremoniy Jan 24 '13 at 13:27
  • What exactly do you want to do? – Rohit Jain Jan 24 '13 at 13:30
  • you are trying to convert a `String` (not a number represent as a String, let say "1", but a letter) to an `int`. That's your mistake, but as already asked: what are you expecting exactly? – ThanksForAllTheFish Jan 24 '13 at 13:34
  • I need to convert text to ints like atoi in c. – menemenemu Jan 24 '13 at 13:35
  • 1
    `atoi` works almost the same as `Integer.parseInt`, except that instead of throwing an exception it reports an error through a `0` return value. Really, if that's what you're after, just catch the exception and return `0` yourself. Although that's a fairly dumb solution, as an exception is much more appropriate here. – Mattias Buelens Jan 24 '13 at 13:38
  • as far i remember (but i'm not expert on math) 'h' is not a number. – user902383 Jan 24 '13 at 13:39
  • Ahh. Then you need to wrap it in some kind of try catch. Check this question: http://stackoverflow.com/questions/1486077/java-good-way-to-encapsulate-integer-parseint Though I should add that I agree with Mattias...if you're passing in an unparsable string, that's a problem, and it should probably throw an exception, rather than just swallow it and return an arbitrary and potentially misleading value. – Beska Jan 24 '13 at 14:01

3 Answers3

3

The String literal "h" obviously does not represent a valid integer.

Assuming h is your String variable, you don't need to use quotes:

String h = "1234";
Integer.parseInt(h);

Update: The function atoi returns an integer value of the input argument and returns zero if the input value is not valid. In Java, you could write this:

public static int javaAToI(String text) {
   try {
      return Integer.parseInt(text);
   } catch (NumberFormatException e) {
      return 0;
   }
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

This will only work if you parse it a String that resembles Integer.

You are parsing it a literal "h" which can not be interpreted as Integer. If you parse it "123" it will work.

Documentation of your method can be found here and documentation is usually a good place to start your search.

As someont else also suggested- if h is your variable, parse it in this way: Integer.parseInt(h);.

Edit:

If you want to convert a char into int, here is your answer: get char value in java

Community
  • 1
  • 1
bjedrzejewski
  • 2,378
  • 2
  • 25
  • 46
0

Java is doing exactly what it is supposed to here. In atoi, when you are returned a 0, how do you know if your string contained a 0, or if it had a string that was not parsable? Java handles this by letting you know that you've tried to parse a string that doesn't contain a parsable value...ideally so you can go back and fix the code that is passing in an unparsable value.

In your case, if you wanted to handle the situation exactly like C does, you could just return a 0 if you catch a NumberFormatException. (It would probably be better if you could go back and ensure that you're not passing in bad strings, but that's not always possible.)

Beska
  • 12,445
  • 14
  • 77
  • 112