【发布时间】:2016-10-09 17:15:11
【问题描述】:
我是 Java 新手,所以请多多包涵。
我的一项任务再次需要帮助。现在涉及到 FileI/O。
我要做的任务是:
我必须阅读 .csv 文件。文件中的值是:
Christopher Lee,54.0
Stanley Wright,90.5
Oliver Stewart,75.8
Jessica Chang,34.65
正如任务所说,我必须将文件上的内容存储到两个数组中。一个用于名称,一个用于测试标记。我应该至少读取文件两次,一次是检查文件中有多少个名称,然后再读取几次以实际读取文件(以获取名称和标记)。所以基本上,我应该有一个数组将名称存储为字符串,还有一个数组将学生的分数存储为实数。
我应该排列数组(例如,students[0] 应该存储第一个学生的姓名,marks[0] 应该存储第一个学生的分数
在将 .csv 文件的内容存储到数组中后,我必须向用户显示以下菜单。如果用户按下 1,它应该提示用户输入学生的姓名。如果用户按下 2,程序应该退出。如果姓名存在,则应显示输入的学生的考试成绩。如果学生不存在,那么我必须向用户输出一条消息指示,但程序不应该结束而是返回到上面的菜单。
这是我目前的代码:
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String fileName = "file:///Documents/Java/marks_file.csv"; // Opens the file
String[] arrayString = new String[6]; // String length inside the file
int numLines, selection = 0;
double[] arrayReal = new double[6]; // Number length inside the file
numLines = getNumLines(fileName); // Gets the length of the file
readFile(arrayString, arrayReal, fileName);
// Selection menu
do
{
System.out.println("Select an option:");
System.out.println("1. Display mark");
System.out.println("2. Exit");
selection = sc.nextInt();
if (selection == 1)
{
System.out.println("Enter your full name");
{
// Do something
}
}
else if (selection == 2)
{
System.out.println("Goodbye");
}
}
while (selection == 1);
//System.out.println("Number of arrays: " + numLines);
}
// Method to get the length of the .csv file
public static int getNumLines(String fileName)
{
FileInputStream fileStrm = null;
InputStreamReader rdr;
BufferedReader bufRdr;
String line;
int lineNum = 0;
try
{
fileStrm = new FileInputStream(fileName);
rdr = new InputStreamReader(fileStrm);
bufRdr = new BufferedReader(rdr);
line = bufRdr.readLine();
while (line != null)
{
lineNum = lineNum + 1;
line = bufRdr.readLine();
}
fileStrm.close();
}
catch (IOException e)
{
try
{
if (fileStrm != null)
{
fileStrm.close();
}
}
catch (IOException ex2)
{
// Nothing to do
}
System.out.println("Error in file processing: " + e.getMessage());
}
return lineNum;
}
// Method to store the values to arrays
public static void readFile(String[] arrayString, double[] arrayReal, String fileName)
{
Scanner sc = new Scanner(System.in);
FileInputStream fileStrm = null;
InputStreamReader rdr;
BufferedReader bufRdr;
String line;
try
{
fileStrm = new FileInputStream(fileName);
rdr = new InputStreamReader(fileStrm);
bufRdr = new BufferedReader(rdr);
for (int i = 0; i < arrayString.length; i++)
{
line = bufRdr.readLine();
arrayString[i] = processString(line);
arrayReal[i] = processReal(line);
}
}
catch (IOException e)
{
try
{
if (fileStrm != null)
{
fileStrm.close();
}
}
catch (IOException ex2)
{
// Nothing to do
}
System.out.println("Error in file processing: " + e.getMessage());
}
}
// Stores the String lines to array
public static String processString(String line)
{
String string;
String[] lineArray = line.split(",");
return string = lineArray[0];
}
// Stores real number lines to array
public static double processReal(String line)
{
double real;
String[] lineArray = line.split(",");
return real = Double.parseDouble(lineArray[1]);
}
到目前为止,我完成了“读取文件”部分并将内容从 .csv 文件处理为数组。
我不太清楚如何提示用户从 .csv 文件中搜索字符串数组。我尝试查看其他来源,甚至在这个网站上,但我一点运气都没有。我尝试了 Scanner.next() 方法,但这根本不起作用。也许我只是错过了一些东西。另外,我不确定我是否正确地“读取文件两次”。
我在正确的轨道上吗?我在这里需要一些指导
【问题讨论】: