如何从Java中用逗号和标题分隔的文件中读取时间?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (96)

我在文件中有一堆列表。我的目标是将所有时间都放在他们自己的字符串数组中time1time2并且time3。该文件如下所示:

time1, 5:01,3:21,4:05,1:52 time2, 2:11,6:35,2:00,5:00 time3, 12:09, 
11:35, 9:02

我试过拆分我读过的每一行然后把它变成标记,用逗号分隔。但是,这似乎不是一个有效的解决方案,因为第一个元素总是以空格开头,而最后一个元素没有逗号。我想知道是否有人知道这个问题的解决方法。

这是我到目前为止提出的代码:

public void read_file(){
try{
    times = new Scanner(new File("times.csv"));
    read_file();
}
catch(Exception e){
    System.out.printf("Could not find file\n");
}                
}
public void read_file(){

while(times.hasNextLine()){
    i++;
    String a = times.nextLine();
    String time[] = a.split(",");
    //would only add first 4 elements
    if(i < 4){
        timeList1.add(time[i])
    }
}
}

这里的问题是我不知道如何检查我必须继续运行多少元素,因为列表中的次数是不可预测的。唯一保持不变的是,总会有3个时间列表被调用time1time2time3

提问于
用户回答回答于

以下是另一种更简单的方法:

public class ReadTimes {
    private Map<String, List<String>> timeLists = new HashMap();
    public void read_file() {
        try {
            Scanner times = new Scanner(new File("times.csv"));
            read_file(times);
        } catch (Exception e) {
            System.out.printf("Could not find file\n");
        }
    }
    public void read_file(Scanner times) {
        String label=null;
        while (times.hasNextLine()) {
            String time[] = times.nextLine().trim().split("[, ]+");
            for (String timeString : time) {
                if (timeString.startsWith("time")) {
                    label = timeString;
                    timeLists.put(label, new ArrayList());
                } else if (label != null) {
                    timeLists.get(label).add(timeString);
                }
            }
        }
        // dump the map of arraylists for demonstration purposes...
        for (Entry<String,List<String>> timeEntry : timeLists.entrySet()) {
            System.out.println(timeEntry);
        }
    }

    public static void main(String[] args) {
        ReadTimes rt = new ReadTimes();
        rt.read_file();
    }
}

鉴于问题中显示的输入数据,将产生以下输出:

time1=[5:01, 3:21, 4:05, 1:52]
time2=[2:11, 6:35, 2:00, 5:00]
time3=[12:09, 11:35, 9:02]
用户回答回答于

既然你相信,你想要的是3字符串数组,下面的代码分裂与“时间”作为分隔符的初始字符串,删除"1, ""2, ""3, "刚开始时,修剪和删除,末和最终除去所有空格后有分裂的每个项目, 作为产生3个字符串数组的分隔符。

    String times = "time1, 5:01,3:21,4:05,1:52, time2, 2:11,6:35,2:00,5:00, time3, 12:09,11:35, 9:02";

    String[] splitted = times.split("time");

    // exclude 0th item which is ""
    for (int i = 1; i < splitted.length; i++) {
        splitted[i] = splitted[i].trim();

        int index = splitted[i].indexOf(" ");

        if (splitted[i].endsWith(","))
            splitted[i] = splitted[i].substring(index + 1, splitted[i].length() - 1);
        else
            splitted[i] = splitted[i].substring(index + 1);

        splitted[i] = splitted[i].replaceAll(" ", "");
    }

    try {  // just in case
        String time1[] = splitted[1].split(",");
        System.out.println(Arrays.toString(time1));
        String time2[] = splitted[2].split(",");
        System.out.println(Arrays.toString(time2));
        String time3[] = splitted[3].split(",");
        System.out.println(Arrays.toString(time3));
    } catch (Exception e) {
        e.printStackTrace();
    }

打印3个字符串数组:

[5:01, 3:21, 4:05, 1:52]
[2:11, 6:35, 2:00, 5:00]
[12:09, 11:35, 9:02]

扫码关注云+社区

领取腾讯云代金券