简体   繁体   中英

Exception in thread “main” java.lang.NumberFormatException: For input string: “”

Original Question

For the following small code I'm getting the error...

import java.io.*;

class test
{

    public static void main(String args[]) throws IOException 
    {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i;

        System.out.println("Enter no of processes ");

        int no_of_process=Integer.parseInt(br.readLine());
        int process[]=new int[no_of_process];

        System.out.println("Enter the values");

        for(i=0;i<no_of_process;i++)
            process[i]=Integer.parseInt(br.readLine());

        for(i=0;i<no_of_process;i++)
            System.out.println(process[i]); 

    }
}

Input:

Enter no of processes 
5
Enter the values
1
2
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at test.main(test.java:17)

Process completed.

I think I have written the code properly and proper integer input is also given. How do I get rid of the above error without using any explicit exception handling statements?

Further Question:

Thanks guys for your answers...it is working. But there is one new question in my mind.

I tried the following modification in the code and executed it. To my surprise, input was accepted properly without any run time error.

for(i=0;i<no_of_process;i++)
    {
        System.out.println(Write anything or even keep it blank);
        process[i]=Integer.parseInt(br.readLine());
    }

By adding just one Print statement before (or even after) the input statement in the for loop, my program worked correctly and no exception was thrown while giving input.

Can you guys explain the reason behind this?

Again if I remove the Print statement from there, the same error gets repeated. I am really confused about the reason behind this. Please help.

Without any error handling statements? check to see if br.readLine() is returning "" before you attempt to parse it, like so:

String line = br.readLine();
if(!String.isEmpty(line))
{
    //Do stuff
}
else
{
    //Do other stuff
}

How to get rid of above error without using any explicit exception handling statements?

for (i = 0; i < no_of_process; i++) {
    String input;
    do {
        input = br.readLine();
        if (isInteger(input)) {
            process[i]=Integer.parseInt(input);
        } else {
            //error handling here
            System.err.println("you entered an invalid input");
        }

    } while(isInteger(input));
}

And isInteger looks like this:

public static boolean isInteger(String s)
{
    try {
        Integer.parseInt(s);
    }
    catch (Exception e) {
        return false;
    }

    return true;
}

I think I have written properly and proper integer input is also given.

I think not ;) i think you pressed the return with out typing anything

Try this:

import java.io.*;

public class test
{

    public static void main(String args[]) throws IOException 
    {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i;

        System.out.println("Enter no of processes ");

       try{
        int no_of_process=Integer.parseInt(br.readLine());
        int process[]=new int[no_of_process];

        System.out.println("Enter the values");

        for(i=0;i<no_of_process;i++)
            process[i]=Integer.parseInt(br.readLine());

        for(i=0;i<no_of_process;i++)
            System.out.println(process[i]); 
       }
       catch(NumberFormatException n)
       {
           System.out.println(n.getMessage());
       }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM