<DataGridTextColumn Header="Rate" Width ="*" Binding="{local:NullableBinding rate}"/>
文本文件如下所示。
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) ;
}
}
答案 0 :(得分:0)
我希望您需要实现的是读取文件,然后将每行添加到ArrayList
String
类型,并将每个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());
}
声明与列表大小相同的数组
String[] studentNames = new String [list.size()];
int[] studentIDs = new int[list.size()];
double[] studentScores = new double[list.size()];
我不推荐这个,因为有时候列表的大小可以 阵列不会接受。
迭代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++;
}
以上所有解释均基于我对您的问题的假设。对不起,如果我错了。