gpt4 book ai didi

java - 如何读取 txt 文件的每一列,并将它们放在单独的数组中?

转载 作者:行者123 更新时间:2023-12-03 23:01:59 25 4
gpt4 key购买 nike

我正在做一个编程作业,基本上我需要从一个 txt 文件中读取并以不同的数组对其中的所有内容进行排序,这样我就可以在 cmd 提示符下整齐地显示所有内容并能够删除内容。

h Vito 123
d Michael 234 Heart
s Vincent 345 Brain Y
n Sonny 456 6
a Luca 567 Business
r Tom 678 Talking Y
j Anthony 789 Maintenance N
d Nicos 891 Bone
n Vicky 911 7

第一列需要是 employeeRole(雇员、医生)。第二列是员工姓名。第三列是employeeNumber,有些还有第四列(如果是数字就是病人的数量。Y代表扫地,接电话)

所以我的想法是将每一列放入它自己的数组中,然后以这种方式写出来。我能够将每一行放入自己的数组中

public class ReadingFile {
// String test;
// char[] employeeRole = new char[9];
String[] employeeRole = new String[9];
String[] employeeName = new String[9], specialty;
String[] wholeLine = new String[9];
// String word;

int[] employeeNum = new int[9];
int r, n, l, num;
public void Reader()
{
Scanner inputStream = null;
Scanner inputStream2 = null;
Scanner inputStream4 = null;
try
{
BufferedReader inputStream3 =
new BufferedReader(new FileReader("data.txt"));
inputStream = new Scanner(new FileInputStream("data.txt"));
inputStream =
new Scanner(new FileInputStream("data.txt"));
inputStream2 =
new Scanner(new FileInputStream("data.txt"));
inputStream4 =
new Scanner(new FileInputStream("data.txt"));
System.out.println("Yeah");

}
catch(FileNotFoundException e){
System.out.println("File Not found");
System.exit(1);
}
for (l=0; l<9; l++)
{
wholeLine[l] = inputStream2.nextLine();

System.out.println(wholeLine[l]);
}

但我无法从那里弄清楚该怎么做。做一个拆分然后将一个数组放入一个数组?这意味着我会将每一行放入一个数组,然后将每个单词放入一个数组?

所以我尝试了其他方法,任何长度不等于 1 的东西都将是 employeeNum,但是他们有 N 和 Y 以及专利数量。

    for(r=0; r<9; r++) //role
{
String next = inputStream4.next();
while( next.length() != 1)
{
next = inputStream4.next();
}

employeeRole[r] = next;

System.out.println(employeeRole[r]);
}

我也试过

for (r=0; r<9; r++)
{
employeeRole[r] = wholeLine[r].substring(wholeLine[r].indexOf(1));
//inputStream.nextLine();
System.out.println(employeeRole[r]);
}

我只是不确定我的做法是否正确?如果我让它变得比实际更困难?或者如果有更简单的方法来做到这一点。但是一切搞定之后,输出应该可以说基本就可以了

Doctors: 2
Name: Michael Employee Number: 234 Specialty: Heart
Name: Nicos Employee Number: 891 Specialty: Bone

非常感谢任何帮助,谢谢!

最佳答案

您不必为了读取文件而打开 4 个流(我猜您想“每列打开一个”,但您不应该这样做)。

其次,您可以在空格 ("") 上拆分字符串,这将完全按照您的需要为您提供列(分别为每一行)。

代码示例:

    BufferedReader br = null;
String[] characters = new String[1024];//just an example - you have to initialize it to be big enough to hold all the lines!

try {

String sCurrentLine;
br = new BufferedReader(new FileReader("data.txt"));

int i=0;
while ((sCurrentLine = br.readLine()) != null) {
String[] arr = sCurrentLine.split(" ");
//for the first line it'll print
System.out.println("arr[0] = " + arr[0]); // h
System.out.println("arr[1] = " + arr[1]); // Vito
System.out.println("arr[2] = " + arr[2]); // 123
if(arr.length == 4){
System.out.println("arr[3] = " + arr[3]);
}

//Now if you want to enter them into separate arrays
characters[i] = arr[0];
// and you can do the same with
// names[1] = arr[1]
//etc
i++;
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

关于java - 如何读取 txt 文件的每一列,并将它们放在单独的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18949861/

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