/* 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()

08-18 03:29
查看更多