• Register
0 votes
588 views

Problem :

I am getting bellow error when I try to use Integer.parseInt() with a single char

exception in thread "main" java.lang.numberformatexception: for input string: "s"

6.9k points
edited by

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

To parse any string to a number you should have valid number into the string.

But your S is not a number.

String s = "s"; 
System.out.println((char) Integer.parseInt(s));

It should be something like bellow code:

String s = "400";
System.out.println((char) Integer.parseInt(s));
38.6k points
edited by
0 votes

Solution:

Reason behind this issue:

The NumberFormatException is one of the most common errors in Java application along with NullPointerException. This error comes when you try to convert a String into numeric data types e.g. int, float, double, long, short, char or byte. The data type conversion methods like Integer.parseInt(), Float.parseFloat(), Double.parseDoulbe(), and Long.parseLong() throws NumberFormatException to signal that input String is not valid numeric value. Even though the root cause is always something which cannot be converted into a number, there are many reasons and input due to which NumberFormatException occurs in Java application. Most of the time I have faced this error while converting a String to int or Integer in Java, but there are other scenarios as well when this error occurs. In this article, I am sharing 10 of the most common reasons of java.lang.NumberFormatException in Java programs.

java.lang.NumberFormatException for input string is one of the most common exceptions java programmers face while doing the coding. This exception occurs when someone tries to convert a String into primitive data types such as int, float, double, long, byte, short, etc. It happens only when the String is not valid to be converted into primitive datatype.

The methods like Integer.parseInt(), Float.parseFloat(), Double.parseDouble() etc. throw java.lang.NumberFormatException when someone tries to convert an invalid String into number.

public class JavaHungry
{
    public static void main(String args[])
    {
        String s = "1";
        int i = Integer.parseInt(s);
        System.out.println(i);
    }
}

The above code is perfect and will not throw any error or exception. The String “1” was successfully converted into Integer 1 because the String was legal to be converted into numerical data.

Now, change the above code a little bit and let see what happens:

public class JavaHungry
{
    public static void main(String args[])
    {
        String s = "1A";
        int i = Integer.parseInt(s);
        System.out.println(i);
    }
}

 

 

31.7k points
edited by

Related questions

0 votes
1 answer 8 views
8 views
Problem: The error received is: Exception in thread main java.lang.numberformatexception for input string
asked May 1 ochena 23.3k points
0 votes
1 answer 489 views
489 views
Problem: exception in thread "awt-eventqueue-0" java.lang.numberformatexception: for input string: ""
asked Feb 18 Lucky Guy 1.5k points
0 votes
1 answer 38 views
38 views
Problem: Exception in thread "main" java.lang.numberformatexception: empty string.
asked May 6 Ryan evans 1.6k points
0 votes
1 answer 25 views
25 views
Problem: I'm trying to figure out a solution, need help >Exception in thread "main" java.lang.numberformatexception.
asked May 9 Florina Gulnar 103k points
1 vote
1 answer 18 views
18 views
Problem: Any solution to this error. Exception in thread "main" java.lang.numberformatexception:
asked Apr 26 PkGuy 25.9k points
0 votes
1 answer 28 views
28 views
Problem: This thing is bugging me, please help > Java lang numberformatexception for input string null.
asked May 16 Irenka Kiri 85.5k points
0 votes
1 answer 40 views
40 views
I’m new. can anyone solve this problem?
asked Dec 14, 2020 TeamScript 13.5k points
1 vote
1 answer 42 views
42 views
Problem: I am a new Java developer. You may call me a student as I am still learning it. I am trying to make a simple Java program. In my program, I tried to taking string input from the user and then store the value in a variable, and afterward, I ... NumberFormatException: For input string: "Name" What is the reason that causes the error? Could any of you please shed some light on this? Thanks.
asked May 30, 2020 adamSw 11.3k points
0 votes
1 answer 23 views
23 views
Problem: What exactly is the solution : Exception in thread "main" java.util.illegalformatconversionexception: f != java.lang.string?
asked Jun 12 Mezbah 125k points
0 votes
1 answer 42 views
42 views
Problem: I have written code for exception. It's building a file but not letting it run, it just throws exception This is code : public static void main(String args[]) { String name = "Umer Hassan"; String name1 = "Hassan Umer"; char[] name2 = new ... at Anagram.main(Anagram.java:24)exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 11, Why is that???
asked Apr 26 nabin 5.1k points