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)
2
  • 1
    "I wanted to purse the int from the string in char position 0 but failed!" <- Can you elaborate what you mean by that? Commented Apr 28, 2017 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? Commented Apr 28, 2017 at 10:56

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

2 Comments

'1' => 49 that means its converting to char('1') to decimal(49) in radix parameter. Why? why to decimal?
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?
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

Comments

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)));   
        }
    }

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.