我需要能够根据csv文件验证用户输入的电子邮件和密码,我必须确保它们在文件中并且它们彼此匹配,例如; ojones@coldmail.net ocrabc。目前我可以拆分文件并将其打印到屏幕上,但我找不到将其放入数组的方法,然后我可以参考。没有阵列有没有更好的方法来解决这样的问题?
import java.io.*;
import java.util.Arrays;
public class CSVRead{
public static void main(String[] arg) throws Exception {
BufferedReader CSVFile =
new BufferedReader(new FileReader("testa453.csv"));
String dataRow = CSVFile.readLine();
while (dataRow != null){
String[] dataArray = dataRow.split(",");
for (String item:dataArray) {
System.out.print(item + "\t");
}
System.out.println();
dataRow = CSVFile.readLine();
}
CSVFile.close();
System.out.println();
}
}
EDIT;只是为了澄清我是Java的新手,并且想要使用和数组,除非有更有效的方法来解决问题;
答案 0 :(得分:2)
您可以创建Map电子邮件 - >密码
import java.io.*;
import java.util.Arrays;
public class CSVRead{
public static void main(String[] arg) throws Exception {
Map<String, String> emailPasswordMap = new HashMap<String, String> ();
BufferedReader CSVFile =
new BufferedReader(new FileReader("testa453.csv"));
String dataRow = CSVFile.readLine();
while (dataRow != null){
String[] dataArray = dataRow.split(",");
emailPasswordMap.put (dataArray[0], dataArray[1]);
dataRow = CSVFile.readLine();
}
CSVFile.close();
String email = "xxx";
String password = "yyy";
if (password.equals (emailPasswordMap.get (email)) {
System.out.println ("Email/password combination is valid");
}
else {
System.out.println ("Email/password combination is invalid");
}
}