Java 文本文件中的多个数组
我绊倒了。我正在为班级做一个项目,在我的一生中,我无法从一个文本文件中创建多个数组。我一直在检查,但我读到的大多数方法都不准确。我能够成功地将文件获取到数组中,但尝试通过hasNext和nextLine创建数组只会得到空数据。有什么建议吗Java 文本文件中的多个数组,java,arrays,Java,Arrays,我绊倒了。我正在为班级做一个项目,在我的一生中,我无法从一个文本文件中创建多个数组。我一直在检查,但我读到的大多数方法都不准确。我能够成功地将文件获取到数组中,但尝试通过hasNext和nextLine创建数组只会得到空数据。有什么建议吗 public static void main (String [] args) throws FileNotFoundException { int count = 0; double sum = 0; String inputf
public static void main (String [] args) throws FileNotFoundException {
int count = 0;
double sum = 0;
String inputfile = "input.txt";
File filename = new File (inputfile);
Scanner console = new Scanner(filename);
ArrayList<String> list = new ArrayList<String>();
while (console.hasNext()) {
list.add(console.nextLine());
}
Scanner user = new Scanner (System.in);
System.out.println("Please verify file before moving forward.");
String fileverify = user.nextLine();
String line = "";
if (fileverify.equals("input.txt")) {
System.out.println("File is available.\n");
}
else {
System.out.println("Wrong file. Please try again.");
return;
}
System.out.println(list);
String[] studentNames = new String [count];
int[]studentIDs = new int[count];
double[]studentScores = new double[count];
count++;
while(console.hasNext()){
studentNames[count] = console.nextLine();
studentIDs[count] = console.nextInt();
studentScores[count] = console.nextDouble();
}
System.out.println("Would you like to:\n" +
"\n1. Print average" +
"\n2. Print high score"+
"\n3. Print low score"+
"\n4. Print median"+
"\n5. Print all student names and scores"+
"\n6. Exit\n");
String command = user.next();
int nextInt = console.nextInt();
if (user.equals("1")){
count++;
sum = sum + console.nextInt();
System.out.println("The average is : " + sum / count) ;
}
}
我希望您需要实现的是读取一个文件,然后将每一行添加到
String
类型的ArrayList
,然后将每个ArrayList
元素拆分为三个不同的数组,并基于这些数组处理数据,如果是这样的话
String inputfile = "input.txt";
File filename = new File (inputfile);
Scanner user = new Scanner (System.in);
System.out.println("Please verify file before moving forward.");
String fileverify = user.nextLine();
if (fileverify.equals("input.txt")) {
System.out.println("File is available.\n");
}
else {
System.out.println("Wrong file. Please try again.");
return;
}
Scanner console = new Scanner(filename);
ArrayList<String> list = new ArrayList<String>();
while (console.hasNext()) {
list.add(console.nextLine());
}
ArrayList
,将元素添加到数组中
for(String line : list) {
String[] lineSplitArray = line.split(":");
studentNames[count] = lineSplitArray[0];
studentIDs[count] = Integer.parseInt(lineSplitArray[1]);
studentScores[count] = Double.parseDouble(lineSplitArray[2]);
count++;
}
以上所有解释都是基于我对你问题的假设。如果我错了,很抱歉。我希望您需要实现的是读取一个文件,然后将每一行添加到
String
类型的ArrayList
中,并将每个ArrayList
元素拆分为三个不同的数组,并基于这些数组操作数据,如果是这样的话
String inputfile = "input.txt";
File filename = new File (inputfile);
Scanner user = new Scanner (System.in);
System.out.println("Please verify file before moving forward.");
String fileverify = user.nextLine();
if (fileverify.equals("input.txt")) {
System.out.println("File is available.\n");
}
else {
System.out.println("Wrong file. Please try again.");
return;
}
Scanner console = new Scanner(filename);
ArrayList<String> list = new ArrayList<String>();
while (console.hasNext()) {
list.add(console.nextLine());
}
ArrayList
,将元素添加到数组中
for(String line : list) {
String[] lineSplitArray = line.split(":");
studentNames[count] = lineSplitArray[0];
studentIDs[count] = Integer.parseInt(lineSplitArray[1]);
studentScores[count] = Double.parseDouble(lineSplitArray[2]);
count++;
}
以上所有解释都是基于我对你问题的假设。如果我错了,很抱歉。你能澄清一下你想要得到什么输出吗?这是一个行数组,其中每一行都是该行的单词数组吗?不要尝试获取一个行数组。更重要的是,我只是想把数组(文本文件)加载到编译器中,这样我就可以按照代码中的说明输出数据了。你能澄清一下你想得到什么样的输出吗?这是一个行数组,其中每一行都是该行的单词数组吗?不要尝试获取一个行数组。更重要的是,我只是尝试将数组(文本文件)加载到编译器中,以便按照代码中的说明输出数据。看起来您没有处理开销信息(第一行信息)。看起来您没有处理开销信息(第一行信息)。