You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@netbeans.apache.org by Zulfi Khan <zu...@yahoo.com.INVALID> on 2021/05/04 04:30:48 UTC

java.lang.NumberFormatException: For input string: ""

Hi,
I am getting following message when I am compiling my program on NetBeans 12.3 on Ubuntu 18.04:
cd /home/zulfi/NetBeansProjects/GUI1; JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 /snap/netbeans/45/netbeans/java/maven/bin/mvn "-Dexec.args=-classpath %classpath com.mycompany.gui1.GuiMain" -Dexec.executable=/usr/lib/jvm/java-11-openjdk-amd64/bin/java process-classes org.codehaus.mojo:exec-maven-plugin:3.0.0:exec
Scanning for projects...

-------------------------< com.mycompany:GUI1 >-------------------------
Building GUI1 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------

--- maven-resources-plugin:2.6:resources (default-resources) @ GUI1 ---
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory /home/zulfi/NetBeansProjects/GUI1/src/main/resources

--- maven-compiler-plugin:3.1:compile (default-compile) @ GUI1 ---
Changes detected - recompiling the module!
Compiling 1 source file to /home/zulfi/NetBeansProjects/GUI1/target/classes

--- exec-maven-plugin:3.0.0:exec (default-cli) @ GUI1 ---
java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.base/java.lang.Integer.parseInt(Integer.java:662)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at com.mycompany.gui1.GuiMain.main(GuiMain.java:21)
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  4.664 s
Finished at: 2021-05-03T23:25:08-05:00
---------------------------------------------
My program is given below:
package com.mycompany.gui1;
import javax.swing.*;
import java.lang.NumberFormatException;

/**
 *
 * @author zulfi
 */
public class GuiMain {
    public static void main(String[] args){
        final String strFirstNum="";
        final String strSecNum="";
        int val1=0;
        int val2=0;
    try{
        val1 = Integer.parseInt(strSecNum);
        val2 = Integer.parseInt(strFirstNum);
    }catch (NumberFormatException e) {
    e.printStackTrace();}
        int res = val1 + val2;
        JOptionPane.showMessageDialog(null, "Testing Sum = "+ res );
        
    }
    
}


Somebody please guide me.
Zulfi.

Re: java.lang.NumberFormatException: For input string: ""

Posted by Laszlo Kishalmi <la...@gmail.com>.
That seems to be right.

On 5/3/21 9:30 PM, Zulfi Khan wrote:
> Hi,
>
> I am getting following message when I am compiling my program on 
> NetBeans 12.3 on Ubuntu 18.04:
>
> cd /home/zulfi/NetBeansProjects/GUI1; 
> JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 
> /snap/netbeans/45/netbeans/java/maven/bin/mvn "-Dexec.args=-classpath 
> %classpath com.mycompany.gui1.GuiMain" 
> -Dexec.executable=/usr/lib/jvm/java-11-openjdk-amd64/bin/java 
> process-classes org.codehaus.mojo:exec-maven-plugin:3.0.0:exec
> Scanning for projects...
>
> -------------------------< com.mycompany:GUI1 >-------------------------
> Building GUI1 1.0-SNAPSHOT
> --------------------------------[ jar ]---------------------------------
>
> --- maven-resources-plugin:2.6:resources (default-resources) @ GUI1 ---
> Using 'UTF-8' encoding to copy filtered resources.
> skip non existing resourceDirectory 
> /home/zulfi/NetBeansProjects/GUI1/src/main/resources
>
> --- maven-compiler-plugin:3.1:compile (default-compile) @ GUI1 ---
> Changes detected - recompiling the module!
> Compiling 1 source file to 
> /home/zulfi/NetBeansProjects/GUI1/target/classes
>
> --- exec-maven-plugin:3.0.0:exec (default-cli) @ GUI1 ---
> java.lang.NumberFormatException: For input string: ""
>     at 
> java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
>     at java.base/java.lang.Integer.parseInt(Integer.java:662)
>     at java.base/java.lang.Integer.parseInt(Integer.java:770)
>     at com.mycompany.gui1.GuiMain.main(GuiMain.java:21)
> ------------------------------------------------------------------------
> BUILD SUCCESS
> ------------------------------------------------------------------------
> Total time:  4.664 s
> Finished at: 2021-05-03T23:25:08-05:00
> ---------------------------------------------
>
> My program is given below:
>
> package com.mycompany.gui1;
> import javax.swing.*;
> import java.lang.NumberFormatException;
>
> /**
>  *
>  * @author zulfi
>  */
> public class GuiMain {
>     public static void main(String[] args){
>         final String strFirstNum="";
>         final String strSecNum="";
>         int val1=0;
>         int val2=0;
>     try{
>         val1 = Integer.parseInt(strSecNum);
>         val2 = Integer.parseInt(strFirstNum);
>     }catch (NumberFormatException e) {
>     e.printStackTrace();}
>         int res = val1 + val2;
>         JOptionPane.showMessageDialog(null, "Testing Sum = "+ res );
>
>     }
>
> }
>
>
> Somebody please guide me.
>
> Zulfi.

RE: java.lang.NumberFormatException: For input string: ""

Posted by John Barrow <jb...@gmail.com>.
Yes, Integer.parseInt() doesn't accept the empty string as a valid number. You would have to initialise the strings to "0" as a default and then would be OK.I think it is a bit of an opinion boundary case in that when I first discovered it, I thought that the empty string should equate to 0, as both of them represent the "zero / null" value. However, I suspect that it was done that way as there is no Integer value that would translate back to the empty string for consistency. I.e. what should String.valueOf(0) return, "0" or "". John
-------- Original message --------From: Zulfi Khan <zu...@yahoo.com.INVALID> Date: 04/05/2021  05:31  (GMT+00:00) To: NetBeans Mailing List <us...@netbeans.apache.org> Subject: java.lang.NumberFormatException: For input string: "" Hi,I am getting following message when I am compiling my program on NetBeans 12.3 on Ubuntu 18.04:cd /home/zulfi/NetBeansProjects/GUI1; JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 /snap/netbeans/45/netbeans/java/maven/bin/mvn "-Dexec.args=-classpath %classpath com.mycompany.gui1.GuiMain" -Dexec.executable=/usr/lib/jvm/java-11-openjdk-amd64/bin/java process-classes org.codehaus.mojo:exec-maven-plugin:3.0.0:execScanning for projects...-------------------------< com.mycompany:GUI1 >-------------------------Building GUI1 1.0-SNAPSHOT--------------------------------[ jar ]------------------------------------ maven-resources-plugin:2.6:resources (default-resources) @ GUI1 ---Using 'UTF-8' encoding to copy filtered resources.skip non existing resourceDirectory /home/zulfi/NetBeansProjects/GUI1/src/main/resources--- maven-compiler-plugin:3.1:compile (default-compile) @ GUI1 ---Changes detected - recompiling the module!Compiling 1 source file to /home/zulfi/NetBeansProjects/GUI1/target/classes--- exec-maven-plugin:3.0.0:exec (default-cli) @ GUI1 ---java.lang.NumberFormatException: For input string: ""    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)    at java.base/java.lang.Integer.parseInt(Integer.java:662)    at java.base/java.lang.Integer.parseInt(Integer.java:770)    at com.mycompany.gui1.GuiMain.main(GuiMain.java:21)------------------------------------------------------------------------BUILD SUCCESS------------------------------------------------------------------------Total time:  4.664 sFinished at: 2021-05-03T23:25:08-05:00---------------------------------------------My program is given below:package com.mycompany.gui1;import javax.swing.*;import java.lang.NumberFormatException;/** * * @author zulfi */public class GuiMain {    public static void main(String[] args){        final String strFirstNum="";        final String strSecNum="";        int val1=0;        int val2=0;    try{        val1 = Integer.parseInt(strSecNum);        val2 = Integer.parseInt(strFirstNum);    }catch (NumberFormatException e) {    e.printStackTrace();}        int res = val1 + val2;        JOptionPane.showMessageDialog(null, "Testing Sum = "+ res );            }    }Somebody please guide me.Zulfi.