Java:从CSV文件中搜索字符串数组
我是新来爪哇的,所以请容忍我
我的一项任务又需要帮助了。现在它涉及到文件I/O
我要做的任务是:
我得读一本书。csv文件。文件中的值包括:
Christopher Lee,54.0
Stanley Wright,90.5
Oliver Stewart,75.8
Jessica Chang,34.65
正如任务所说,我必须将文件中的内容存储到两个数组中。一个是名字,一个是测试分数。我应该至少读两次文件,一次检查文件中有多少个名字,再读几次以实际读取文件(获取名字和标记)。所以基本上,我应该有一个数组,将名字存储为字符串,还有一个数组,将学生的分数存储为实数
我应该排列数组(例如,学生[0]应该存储第一个学生的名字,马克[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文件。我试图寻找其他来源,甚至在这个网站上,但我一点运气都没有。我试过扫描仪。next()方法,但这根本不起作用。也许我错过了什么。此外,我不确定我是否正确地“读取了两次文件”
我走对了吗?我需要一些指导
# 1 楼答案
与其将数据存储到数组中,不如告诉您将数据传递到通用arraylist中,然后使用get()方法查询结果