簡體   English   中英

線程“ main”中的異常java.lang.NumberFormatException:對於輸入字符串:“”

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

原始問題

對於以下小代碼,我得到了錯誤...

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]); 

    }
}

輸入:

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.

我想我已經正確編寫了代碼,並且還給出了正確的整數輸入。 如何在不使用任何顯式異常處理語句的情況下擺脫上述錯誤?

進一步的問題:

謝謝大家的回答...它正在工作。 但是我心中有一個新問題。

我在代碼中嘗試了以下修改並執行了它。 令我驚訝的是,輸入被正確接受,沒有任何運行時錯誤。

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

通過在for循環中的輸入語句之前(甚至之后)添加一個Print語句,我的程序可以正常工作,並且在提供輸入時不會引發異常。

你們能解釋這背后的原因嗎?

同樣,如果我從此處刪除Print語句,則會重復出現相同的錯誤。 我真的很困惑背后的原因。 請幫忙。

沒有任何錯誤處理語句? 在嘗試解析br.readLine()之前先檢查它是否返回“”,如下所示:

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

如何在不使用任何顯式異常處理語句的情況下擺脫上述錯誤?

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

isInteger看起來像這樣:

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

    return true;
}

我想我寫的正確,並且給出了正確的整數輸入。

我認為不是;)我認為您沒有輸入任何內容就按了返回鍵

嘗試這個:

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());
       }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM