繁体   English   中英

Ideone Java程序上的运行时错误

[英]Runtime error on Ideone Java Program

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class time
{
    int h,m,sec;
    void getdata()throws IOException
    { /*Scanner in = new Scanner(System.in);`
      h=in.nextInt();
      m= in.nextInt();
      s=in.nextInt();*/
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String s= br.readLine();
        String str[] = s.split(" ");

        h=Integer.parseInt(str[0]);
        m=Integer.parseInt(str[1]);
        sec=Integer.parseInt(str[2]);
    }
    int calc_time(time t1,time t2)
    { return ((t2.h - t1.h)*3600 + (t2.m - t1.m)*60 + (t2.sec - t1.sec));
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        time t = new time();
        t.getdata();
        time te = new time();
        te.getdata();
        time tt = new time();
        int val = tt.calc_time(t,te);
        if(val>=0&&val<=99) System.out.println("S");
        else if(val>99&&val<=199) System.out.println("C");
        else if(val>199 && val<=299) System.out.println("S");
        else if(val>299 && val<=399) System.out.println("C");

    }
}

尽管Ideone上的该特定程序在我的PC上运行正常,但出现运行时错误。 它指示第二个对象对split函数和getdata()函数的调用出错。

http://ideone.com/qpOO9R

您到达STDIN的流已结束,这意味着在运行以下命令之后:

  String s = br.readLine();

s仍然为null,这将在以下行上引起NPE:

String str[] = s.split(" ");

要解决此问题,请检查null并做一些合理的事情,并提供一些输入供Ideone使用。 此外,您的BufferedReader每次执行仅应创建一次,如该ideone草图所示: http ://ideone.com/VtQrxk

相关的javadoc:

公共字符串readLine()引发IOException

读取一行文本。 一行被认为由换行符('\\ n'),回车符('\\ r')或回车符后立即换行符中的任何一个终止。

返回:

一个字符串,其中包含行的内容,不包含任何行终止符;如果已到达流的末尾,则为null

http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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