• Register
0 votes
394 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 5 3
6,930 points

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));
9 7 4
38,600 points
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);
    }
}

 

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 10 views
10 views
Problem: I am working on the knapsack problem, I am new to Java. I am able to add numbers manually like this in the main: // Fill the bag of weights. //myWeights.bagOfWeights.add(18); //myWeights.bagOfWeights.add(2); //System.out.println("Possible answers: "); // ... the weights. So I have tried to take the user input as a string and split it up with whitespace, and then convert it to an integer.
asked Feb 2 Ali 6.2k points
0 votes
1 answer 21 views
21 views
Problem: exception in thread "awt-eventqueue-0" java.lang.numberformatexception: for input string: ""
asked Feb 18 Lucky Guy 1.4k points
0 votes
1 answer 5 views
5 views
Problem: Why I got the error : exception in thread "main" java.lang.numberformatexception: empty string How to fix this erro:exception in thread "main" java.lang.numberformatexception: empty string Can someone please help fix this, exception in thread "main" java.lang.numberformatexception: empty string Any ideas on what I need to do to fix this? Thanks!
asked 5 days ago Ali 6.2k points
0 votes
1 answer 15 views
15 views
Prblem: I am working on the knapsack problem, I am new to Java. I am able to add numbers manually like this in the main: // Fill the bag of weights. //myWeights.bagOfWeights.add(18); //myWeights.bagOfWeights.add(2); //System.out.println("Possible answers: "); // ... it up with whitespace, and then convert it to an integer. Next, I tried to do the parseInt 2 ways, but I was unsuccessful both way.
asked Feb 19 Ali 6.2k points
0 votes
1 answer 18 views
18 views
Problem: I have done: cut -f FR07937945--FR07937944_gridss.vcf.gridss_RPostProcess.bedpe and confirmed that all of the second column (QUAL) are digits. Some are integers, but most are floats. Since line 387 of Event.java is: int p1 = Integer.parseInt(bits[1]); I guess there is an issue ... floats in the QUAL col, or is it that our file isn't a bedpe file, or is it that this is an error in clove?
asked Mar 23, 2020 LizzyM 6.1k points
0 votes
1 answer 7 views
7 views
I’m new. can anyone solve this problem?
asked Dec 14, 2020 TeamScript 13.5k points
1 vote
1 answer 29 views
29 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 3 views
3 views
Problem: Why I got the error : exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 1 HELP! I need help understanding this error. Can someone please help fix this, I am new in java: exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 1 Any anyone ideas on what I need to do to fix this? Thanks!
asked 4 days ago Ali 6.2k points
0 votes
1 answer 12 views
12 views
Problem: inputs the amount of minutes the user enters and calculates the grand total by adding the CostForMinutes and BasePlanCost. CostForMinutes is calculated by multiplying the minutes the user enters and the BaseCostPerMinute. The out is all the numbers outputted ... this error exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String can anyone help me out?
asked Feb 22 Ali 6.2k points
0 votes
1 answer 8 views
8 views
Problem: I'm getting the exception java.lang.ClassNotFoundException when I am trying to run my code, I'm getting Error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native ... ) at java.lang.Class.forName(Class.java:169) at Simple.MyProg.main(MyProg.java:15) What am I doing wrong?
asked Feb 22 Ali 6.2k points