gpt4 book ai didi

java - 将文本文件行中的字符串标记输入数组,然后作为数组集合输出

转载 作者:行者123 更新时间:2023-11-30 06:32:19 24 4
gpt4 key购买 nike

我想逐行读取文本文件。文本文件中的每一行包含 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
...

最佳答案

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

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();
}

关于java - 将文本文件行中的字符串标记输入数组,然后作为数组集合输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com