2
import java.util.*;

public class test 
{
    public static void main(String[] args)
    {
        int i = 123;

        String s = Integer.toString(i);

        int Test = Integer.parseInt(s, s.charAt(0));    // ERROR!
    }
}

I want to parse the input string based on char position to get the positional integer.

Error message:

Exception in thread "main" java.lang.NumberFormatException: radix 49 greater than Character.MAX_RADIX
    at java.lang.Integer.parseInt(Unknown Source)
    at test.main(test.java:11)
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Anna
  • 319
  • 5
  • 18
  • 1
    "I wanted to purse the int from the string in char position 0 but failed!" <- Can you elaborate what you mean by that? – OH GOD SPIDERS Apr 28 '17 at 10:56
  • You realize that the second parameter to `parseInt(String, int)` is the radix, i.e. the base of the number? What would a base-1 number be? I assume that "error" you get has a message like `radix 1 less than Character.MIN_RADIX` (with min radix being 2), hasn't it? – Thomas Apr 28 '17 at 10:56

4 Answers4

5

That method you are calling parseInt(String, int) expects a radix; something that denotes the "number system" you want to work in, like

parseInt("10", 10) 

(10 for decimal)! Instead, use

Integer.parseInt(i)

or

Integer.parseInt(i, 10)

assuming you want to work in the decimal system. And to explain your error message - lets have a look at what your code is actually doing. In essence, it calls:

Integer.parseInt("123", '1') 

and that boils down to a call

Integer.parseInt("123", 49) // '1' --> int --> 49! 

And there we go - as it nicely lines up with your error message; as 49 isn't a valid radix for parsing numbers.

But the real answer here: don't just blindly use some library method. Study its documentation, so you understand what it is doing; and what the parameters you are passing to it actually mean.

Thus, turn here and read what parseInt(String, int) is about!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • '1' => 49 that means its converting to char('1') to decimal(49) in radix parameter. Why? why to decimal? – Anna Apr 28 '17 at 15:39
  • You pass the value '1'.but converting that char results in an int value of 49. Just Google for ASCII table, and look up the numerical value of '1'. You take the first char in "123" and use that as second argument for the method call. So your code sees 49 as radix. Now clear? – GhostCat Apr 28 '17 at 16:51
1

Integer.parseInt(parameter) expects the parameter to be a String. You could try Integer.parseInt(s.charAt(0) + ""). The +"" is to append the character to an empty String thereby casting the char to String and this is exactly what the method expects.

Another method to parse Characters to Integers (and in my opinion much better!) is to use Character.getNumericValue(s.charAt(0));

Check this post for further details on converting char to int

Community
  • 1
  • 1
goerlibe
  • 120
  • 1
  • 12
0

Need to convert String.valueOf(s.charAt(0)) to String.valueOf(s.charAt(0)) i.e. Char to String.

import java.util.*;

    public class test 
    {
        public static void main(String[] args)
        {
            int i = 123;

            String s = Integer.toString(i);
            int Test = Integer.parseInt(String.valueOf(s.charAt(0)));   
        }
    }
SiddP
  • 1,603
  • 2
  • 14
  • 36
0

Let use what we have here.

To parse one digit from a String into an integer. Use getNumericValue(char) In your case, to get the first character into a number :

int n = Character.getNumericValue(s.charAt(0));

Be aware that you should take the absolute value if you integer can be negative.

AxelH
  • 14,325
  • 2
  • 25
  • 55