编写一个程序,从文件中读取时间表并打印到屏幕上?

时间:2013-03-10 23:19:47

标签: java arraylist

保存为文本文档的初始格式: 表1:

04
05 10 25 40 55 
06 05 15 25 35 45 55 
07 00 05 10 15 20 25 30 35 40 45 50 55
08 00 05 10 15 20 25 30 35 40 45 50 55
09 00 05 10 15 20 25 30 35 40 45 50 55 
10 00 05 10 15 20 25 30 35 40 45 50 55 
11 00 05 10 15 20 25 30 35 40 45 50 55 
12 00 05 10 15 20 25 30 35 40 45 50 55 
13 00 05 10 15 20 25 30 35 40 45 50 55 
14 00 05 10 15 20 25 30 35 40 45 50 55 
15 00 05 10 15 20 25 30 35 40 45 50 55 
16 00 05 10 15 20 25 30 35 40 45 50 55
17 00 05 10 15 20 25 30 35 40 45 50 55
18 00 05 10 15 20 25 30 35 40 45 50 55 
19 00 05 10 15 20 25 30 35 40 45 50 55 
20 00 05 10 15 20 25 30 35 40 45 50 55 
21 05 15 25 40 55 
22 10 25 40 55 
23 10 25 40 55 55 
00 10 25 40 55 
01 17 47 
02 17 47 
03 17 47

所需格式应如下所示: 表2:

05:10 05:25 05:40 05:55 
06:05 06:15 06:25 06:35 06:45 06:55 
07:00 07:05 07:10 07:15 07:20 07:25 07:30 07:35 07:40 07:45 07:50 07:55 
08:00 08:05 08:10 08:15 08:20 08:25 08:30 08:35 08:40 08:45 08:50 08:55 
09:00 09:05 09:10 09:15 09:20 09:25 09:30 09:35 09:40 09:45 09:50 09:55 
10:00 10:05 10:10 10:15 10:20 10:25 10:30 10:35 10:40 10:45 10:50 10:55 
11:00 11:05 11:10 11:15 11:20 11:25 11:30 11:35 11:40 11:45 11:50 11:55 
12:00 12:05 12:10 12:15 12:20 12:25 12:30 12:35 12:40 12:45 12:50 12:55 
13:00 13:05 13:10 13:15 13:20 13:25 13:30 13:35 13:40 13:45 13:50 13:55 
14:00 14:05 14:10 14:15 14:20 14:25 14:30 14:35 14:40 14:45 14:50 14:55 
15:00 15:05 15:10 15:15 15:20 15:25 15:30 15:35 15:40 15:45 15:50 15:55 
16:00 16:05 16:10 16:15 16:20 16:25 16:30 16:35 16:40 16:45 16:50 16:55 
17:00 17:05 17:10 17:15 17:20 17:25 17:30 17:35 17:40 17:45 17:50 17:55 
18:00 18:05 18:10 18:15 18:20 18:25 18:30 18:35 18:40 18:45 18:50 18:55 
19:00 19:05 19:10 19:15 19:20 19:25 19:30 19:35 19:40 19:45 19:50 19:55 
20:00 20:05 20:10 20:15 20:20 20:25 20:30 20:35 20:40 20:45 20:50 20:55 
21:05 21:15 21:25 21:40 21:55 
22:10 22:25 22:40 22:55 
23:10 23:25 23:40 23:55 23:55 
00:10 00:25 00:40 00:55 
01:17 01:47 
02:17 02:47 

为什么输出不打印时间如图2所示?这就是我到目前为止所写的内容。我有一个主类,它从table1读取并将文本存储到arrayList。课堂时间以所需的格式发送时间和小时。

public static void main(String[] args) {

        Scanner scanner = null;

        try {

        File file = new File("C:/Users/Tommy/workspace/Prov/SLtid.txt");
        scanner = new Scanner(file);
        while(scanner.hasNextLine()){
            String[] tokens = scanner.nextLine().split("\\s+");

                for (int i= 0; i< tokens.length; i++)
                {

                    Time t = new Time(tokens[i], tokens[i].substring(01));

                // new time ( hour, minute )


            System.out.println(t);  
                }

        }} catch (Exception e) { 
            e.printStackTrace();
        }}}

我有另一个叫做时间类的类,它基本上将小时和分钟发送到主类:

public class Time {

        int hr;
        int minute;

        Time(String t, String m){

        hr = Integer.parseInt(t);
        minute = Integer.parseInt(m);

    }



    public String toString(){

        return String.format("%02d:%02d", hr, minute);


}}

1 个答案:

答案 0 :(得分:1)

这一行:

Time t = new Time(tokens[i], tokens[i].substring(01));

没有向Time构造函数提交正确的参数。根据您的输入,似乎小时始终是第一个令牌,但您不是以这种方式提交,因此您最终会提交分钟而不是小时。尝试:

for (int i= 1; i < tokens.length; i++) {
    Time t = new Time(tokens[0], tokens[i]);
}

或类似的东西。