如何验证从CSV备份的输入数据,没有专用的CSV解析器 [英] How to validate inputted data from that backed up with a CSV without a purpose built CSV parser
问题描述
我需要能够验证用户输入的电子邮件和密码对一个csv文件,我有,必须确保他们在文件中,并且他们彼此匹配,如;
ojones@coldmail.net ocrabc。目前,我可以拆分文件,并将其打印到屏幕,但我找不到一个方法把它放入一个数组,然后我可以参考。有没有更好的方法来解决这样的问题没有数组?
I need to be able to verify a user inputted email and password against a csv file which I have that have to make sure that they are in the file and also that they match each other such as; ojones@coldmail.net ocrabc. Currently I can split the file and print it to the screen but I cannot find a way to put it into an array which I can then refer to. Is there a better way of solving such a problem without an array?
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并不陌生,并希望使用和数组,除非有更有效的方法来解决问题;
EDIT; Just to clarify I am reasonably new to Java and would like to use and array unless there is a more efficient way of solving the problem;
推荐答案
您可以创建地图电子邮件 - >密码
You can create a Map of e-mail -> password
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");
}
}
这篇关于如何验证从CSV备份的输入数据,没有专用的CSV解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!