• Register
search
Log In
0 votes
147 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

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

 

 

12.8k points
edited by

Related questions

0 votes
1 answer 11 views
11 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 LizzyM 5.7k points
1 vote
1 answer 12 views
12 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 adamSw 8.4k points
1 vote
1 answer 17 views
17 views
Problem: I am a begginer in java programming language and I have been coding for just some days. To practice my programming I wrote this code in java: public class Main { public static void main(String[] argv){ int 2count, h-l, a/b, } } But It shows me the error message ... , . expected Syntax error on token ",", ;expected at Main.main(Main.java:3) Whats is wrong in my code can anyone help me out?
asked Apr 6 LizzyM 5.7k points
1 vote
1 answer 16 views
16 views
Problem : I have very recently started to learn programming in Java. I am trying to execute my java code but I am facing the below error. “Exception in thread "main" java.lang.arrayindexoutofboundsexception:” I am unable to understand above error. Please find below my code: ... according to my debugger*/ myodd[j] = input[i]; j++; } return myodd; } Please help me in fixing above error.
asked Mar 30 morrisBson 3.2k points
1 vote
1 answer 22 views
22 views
Problem: I got error while applying weblogic patch useing bsu.sh: patch_download_dir=$FMW_HOME/utils/bsu/cache_dir -patchlist=GFWX -prod_dir=$FMW_HOME/wlserver_10.3 -log=$FMW_HOME/utils/bsu/bsu_weblogic.log Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded ... .bea.plateng.patch.Patch.main(Patch.java:281) Can anyone explain me this and help me to solve this error?
asked Mar 27 LizzyM 5.7k points