简体   繁体   English

Java 读取列文件中具有不同数字的txt并将数据存储在arraylist中

[英]Java Reading a txt that has different numbers in its column file and store data in an arraylist

Hello Guys I have a txt file below, I need to store the second column data but it gives me an error since some lines have 1 some have 2 and some have 3 input in each line.大家好,我在下面有一个 txt 文件,我需要存储第二列数据,但它给了我一个错误,因为有些行有 1,有些有 2,有些每行有 3 个输入。 How can I solve that problem?我该如何解决这个问题?


5
3 4
3 4
3 3
3 4
3 3
3 4
3 3
3 2
3 4
3 3
3 2
3 1
3 4
3 3
3 2
3 1
3 0
1
2
5 3 4
3 4
3 4
3 3
3 4
3 3
3 4
3 3
3 2
3 4
3 3
3 2
3 1
3 4
3 3
3 2
3 1
3 0
1
2
5 4 6
4 4
4 4
4 4
4 4
4 4
4 4
4 3
4 3
4 3
4 3
4 4
4 4
1
2
5 4 6
0

Here is what I did, I tried to differentiate as the size and in the other way but still cant get the answer...这就是我所做的,我试图区分大小和其他方式,但仍然无法得到答案......

String line = "";

ArrayList<String> numbers= new ArrayList<String>();

try {

    String sCurrentLine;
    br = new BufferedReader(new FileReader("input1.txt"));
    int n = 0;

    while ((sCurrentLine = br.readLine()) != null) {
        String[] arr = sCurrentLine.split(" ");
        int size = arr.length;

        List<String> list = ConvertToList.convertArrayToList(arr);
        List<Integer> listOfInteger = convert.convertStringListToIntList(list, Integer::parseInt);
        if (list.size() == 2) {
            line.split("\\s+");
            numbers.add(line.split("\\s+")[0]);
            System.out.println(numbers);
        }
    }
} catch(Exception e) {
    e.printStackTrace();
}

You do not need to split the line again.您无需再次拆分线路。 When you split the line first time, check if the length of the resulting array is 2 .第一次拆分行时,检查结果数组的长度是否为2 If yes, add arr[1] to numbers .如果是,请将arr[1]添加到numbers

while ((sCurrentLine = br.readLine()) != null) {
    String[] arr = sCurrentLine.split(" ");                
    if (arr.length >= 2) {
        numbers.add(arr[1]);
        System.out.println(numbers);
    }
}

Update:更新:

Based on your comment, I am providing you following code which uses your lists:根据您的评论,我为您提供以下使用您的列表的代码:

while ((sCurrentLine = br.readLine()) != null) {
    String[] arr = sCurrentLine.split(" ");
    List<String> list = ConvertToList.convertArrayToList(arr);
    List<Integer> listOfInteger = convert.convertStringListToIntList(list, Integer::parseInt);
    if (listOfInteger.size() >= 2) {
        numbers.add(listOfInteger.get(1));
    }          
}
System.out.println(numbers);

You can keep System.out.println(numbers);您可以保留System.out.println(numbers); inside or outside the while loop depending on how you want to print numbers .while循环内部或外部,具体取决于您要如何打印numbers

If you want to read and save only the second column than you get the data in second column by using the index 1. (After your while loop.)如果您只想读取和保存第二列而不是使用索引 1 获取第二列中的数据。(在您的 while 循环之后。)

String secondCol = sCurrentLine.split(" ")[1];

In case you don't have a value in 2 column than it will throw and exception which you should handle with simple try catch.如果您在 2 列中没有值,则会抛出异常,您应该使用简单的 try catch 处理。 Finally convert it to int and store in your list.最后将其转换为 int 并存储在您的列表中。 with following.与以下。

listOfInteger.add(Integer.parseInt(secoundCol));

Hopefully it will work.希望它会奏效。

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

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