我已经制作了一段代码,用于读取包含此信息的csv文件。它包含学生姓名和考试成绩。
Christopher Lee,54.0
Stanley Wright,90.5
Oliver Stewart,75.8
Jessica Chang,34.65
在文件中读取时,文件名由用户输入为String。 文件的内容必须同时存储在两个数组中,其中学生名称将存储为字符串,测试标记将存储为实数。我有一个选择菜单,有两个选项,显示标记和退出。当选择“显示标记”时,用户输入学生的名称,程序在数组中找到名称的索引号,然后程序打印出相同的索引号但在包含测试标记的数组中,从而打印出来相应的测试标记。
我已经完成了大部分代码,但我需要一些帮助来排除故障并修复IO逻辑。我是文件IO的新手。感谢
以下是代码:
import java.util.*;
import java.io.*;
public class MarksIO
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String fileName;
fileName = sc.nextLine();
boolean valid;
valid = false;
while (valid)
{
int choice = 0;
System.out.println("Select an Option: ");
System.out.println("Display Mark = 1.\nExit = 2.");
if (choice == 1)
{
System.out.println("Enter the name of the Student.");
String name;
name = sc.nextLine();
int lines = getNumLines(fileName);
String[] arrayString = new String[lines];
double[] arrayReal = new double[lines];
readFile(arrayString, arrayReal, fileName);
System.out.println("Test Mark is: " + arrayReal[index]);
}
else if (choice == 2)
{
System.exit(0);
}
}
}
public static int indexCalc(String name, double[] arrayReal,
String[] arrayString)
{
for (int index = 0; index < arrayString.length; index++)
{
if (arrayString[index].equals(name))
{
return index;
}
}
}
public static void readFile(String fileName, double[] arrayReal,
String[] arrayString)
{
FileInputStream fileStream = null;
InputStreamReader reader;
BufferedReader bufferedReader;
int lineNum;
String line;
try
{
fileStream = new FileInputStream(fileName);
reader = new InputStreamReader(fileStream);
bufferedReader = new BufferedReader(reader);
for (int i = 0; i <= arrayString.length; i++)
{
lineNum = 0;
line = bufferedReader.readLine();
while (line != null)
{
lineNum++;
arrayString[i] = processString(line);
arrayReal[i] = processReal(line);
}
}
fileStream.close();
}
catch (IOException ex)
{
if (fileStream != null)
{
try
{
fileStream.close();
}
catch (IOException ex2)
{
}
System.out.println("Error in file processing: "
+ ex.getMessage());
}
}
}
public static String processString(String line, String[] arrayString)
{
String[] stringArray = line.split(",");
String string = stringArray[0];
return string;
}
public static double processReal(String line, double[] arrayReal)
{
double[] realArray = line.split(",");
double real;
real = (double)(realArray[1]);
return real;
}
public static int getNumLines(String fileName)
{
FileInputStream fileStream;
InputStreamReader reader;
BufferedReader bufferedReader;
try
{
fileStream = new FileInputStream(fileName);
reader = new InputStreamReader(fileStream);
bufferedReader = new BufferedReader(reader);
}
catch (FileNotFoundException e)
{
System.out.println("Error in file processing: " + e.getMessage());
}
int numLines;
numLines = 0;
int lineNum = 0;
String line = bufferedReader.readLine();
while (line != null)
{
lineNum++;
numLines = numLines + 1;
line = bufferedReader.readLine();
}
}
}
答案 0 :(得分:0)
您可以使用Scanner类完成此任务。喜欢
import java.io.*;
import java.util.Scanner;
public class ScanXan {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
while (s.hasNext()) {
System.out.println(s.next());
}
} finally {
if (s != null) {
s.close();
}
}
}
}