2

我需要能够根据我拥有的 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();

}

}

编辑; 只是为了澄清我对Java相当陌生,并且希望使用和数组,除非有更有效的方法来解决问题;

4

1 回答 1

2

您可以创建电子邮件地图-> 密码

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");
    }
}
于 2012-11-13T21:32:17.877 回答