【问题标题】:Input string tokens from text file's lines into array, then output as collection of arrays将文本文件行中的字符串标记输入数组,然后作为数组集合输出
【发布时间】:2018-02-01 16:00:21
【问题描述】:

我想逐行读取文本文件。文本文件中的每一行包含 5 个字符串 - 以“”分隔。从行中拆分每个字符串标记后,我必须将每个标记放入一个数组中。只有这样我才能创建此类数组的数组列表。

我在打印 ArrayList 时遇到问题,因为循环似乎无限运行并且只打印文件中最后一行(最后一个数组)上的字符串。

我做错了什么?

这是我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test 
{
    static File file = new File("C:\\Users\\Nuno\\Documents\\test_file.txt");          //source file

    private static String[] values = new String[4];                                    //array of 5 values (numbers as string values)
    private static final List<String[]> ListOfArrayOfValues = new ArrayList<>();       //ArrayList of String arrays

    public static void ReadWriteArrayList()
    {
        String lines;                                                           //each line on file, that contains 5 nums

        try                                                                     //read data from file and create ArrayList containing arrays of 5 strings
        {   
            FileReader fileReader = new FileReader(file);                       //reads file in default encoding
            BufferedReader bufferReader = new BufferedReader(fileReader);       //always wrap FileReader in BufferedReader
            //lines = bufferReader.readLine();                                  //this assignment must be argument to while loop, otherwise goes in infinite loop 
            while((lines = bufferReader.readLine()) != null)       
            {
                values = lines.split(" ");                                      //assign each string token in the line (split by space) to an ARRAY of Strings
                ListOfArrayOfValues.add(values);                                //add each array to the arraylist
            }
            bufferReader.close();                                               //always close file after reading
        }catch (IOException ex) 
        {
            Logger.getLogger(DataLott.class.getName()).log(Level.SEVERE, null, ex);
        }

        //read ArrayList and output arrays (drawings) to screen
        ListIterator<String[]> outputArrayOfValues = ListOfArrayOfValues.listIterator();    //iterator for ArrayList
        while(outputArrayOfValues.hasNext())                                                //iterate through ArrayList
        {
            for(String num : values)                                            //loop each array in ArrayList
            {
                System.out.println(num + " ");                                  //print each number in a array (i.e. each number in a drawing)
            }
        }
    }

    //main method
    public static void main(String[] args) 
    {
        ReadWriteArrayList();
    }

}

输出应该看起来像原始文件,例如:

10 28 31 33 34
02 15 20 29 31
10 19 23 25 34
03 04 08 33 34

相反,我得到:

03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
...

【问题讨论】:

    标签: java arrays list arraylist iterator


    【解决方案1】:

    您需要从循环内的迭代器中检索数组,并更改打印值的方式:

    ListIterator<String[]> outputArrayOfValues = ListOfArrayOfValues.listIterator();
    while(outputArrayOfValues.hasNext())                                                    {
    
        // add this line to get the values from the current item in the iterator
        String[] rowValues = outputArrayOfValues.next();
    
        for(String num : rowValues)                                                    
        {
            //  change this line to not print a carriage return
            System.out.print(num + " ");                                  
        }
    
        // print the carriage return here
        System.out.println();
    }
    

    【讨论】:

      【解决方案2】:

      您应该使用 System.out.print(num + " "); (没有 ln)在内部循环中,然后是 system.out.println("") 在外部 while 循环中。 (ln 是换行符)。

      【讨论】:

      • 哦,我明白了。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多