从键盘读取文本时java.lang.NumberFormatException

时间:2017-07-28 21:10:27

标签: numberformatexception

这个程序从键盘读取数字并对其求和,直到用户写完"总计",然后我得到java.lang.NumberFormatException

Boolean isTotal = false;
    int sum = 0;
    while(!isTotal)
    {
        java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
        String s = br.readLine();
        if(s=="total")
        {
            isTotal = true;
        }
        if(!isTotal)
        sum = sum + Integer.parseInt(s);
    }
    System.out.println(sum);

1 个答案:

答案 0 :(得分:0)

Boolean isTotal = false;
int sum = 0;
while(!isTotal)
{
    java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
    String s = br.readLine();
    if(s.equals("total"))
    {
        isTotal = true;
    }
    if(!isTotal)
    sum = sum + Integer.parseInt(s);
}
System.out.println(sum);