导入CSV文件并比较用户输入

时间:2012-10-05 13:05:10

标签: java csv io bufferedreader

开始一个项目,这让我很开心。我认为代码没问题,但我不明白如何返回记录或解析该行。

我有一个CSV文件,在Libra中显示如下。

USER PASSWORD 克里斯密码 米歇尔密码 约翰密码

我让用户输入他们的用户和密码,然后尝试将这些用户和密码进行比较,以确保1)用户名存在,2)如果确实存在,则说明密码是正确的。说实话,第2部分并不重要,因为它不是规范的一部分,但我想这样做,我怀疑它与我所拥有的问题是一样的。正如我所说的,我认为代码没问题,但我不确定thisLine变量的格式是什么,是我的else语句正确地将BufferedReader移动到下一行?

我可以使用thisLine.trim()将USER PASSWORD剪切为USER吗?

static void readFromCsvFile(String sFileName,User user)抛出FileNotFoundException        {            String thisLine;

        try
        {
           BufferedReader reader = new BufferedReader(new FileReader(sFileName));
           thisLine = reader.readLine();
           System.out.print(thisLine);

           while((thisLine = reader.readLine()) != null)
               {
                    if (user.displayUserName() == thisLine)
                    {
                    System.out.print("\nUser <-" + user.displayUserName() + " -> exists!");
                    reader.close();
                    }

                    else 
                    {
                        thisLine = reader.readLine();
                    }
               }

        }
        catch(IOException e)
        {
            System.out.print("\nUser does not exist\n"); 
            e.printStackTrace();
        } 

2 个答案:

答案 0 :(得分:2)

这里有几点评论:

1)thisLine.trim()将删除thisLine内容开头和结尾的尾随空白。这样做是可以的,特别是如果你要比较两个字符串,但它不会从变量中分割用户名和密码。

2)要分割两个不同的值,您应该使用thisLine.split(" ")(我假设您的CSV文件使用空格来分隔不同的字段)。

3)另一个错误是使用==而不是equals来比较字符串,这是正确的方法。

4)由于您正在while条件下阅读新内容,因此您不需要内部reader.readLine()

5)最后,永远不要关闭循环内的流(或读取器)!!在try/catch/finally区块上进行。

因此,通过这些更正,您的代码将如下所示:

 static void readFromCsvFile(String sFileName, User user) throws FileNotFoundException { 
   String thisLine;
   BufferedReader reader = new BufferedReader(new FileReader(sFileName));
    try
    {

       thisLine = reader.readLine();
       System.out.print(thisLine);

       while((thisLine = reader.readLine()) != null)               
           {
               thisLine = thisLine.trim();
               String username = thisLine.split(" ")[0];
                if (user.displayUserName().equals(username))
                {
                System.out.print("\nUser <-" + user.displayUserName() + " -> exists!");
                break;   // break the loop
                }                    
           }

    }
    catch(IOException e)
    {
        System.out.print("\nUser does not exist\n"); 
        e.printStackTrace();
    } 
    finally {
        try {
            reader.close();
        } catch (IOException e) { /* ignore */ }
    }
}

答案 1 :(得分:1)

比较两个字符串:

firstString.equal(secondString);

另一个问题是thisLine包含用户名和密码(例如“Chris Password”),因此您必须将您的行拆分为单独的用户名(“Chris”)和密码(“密码”)。

while((thisLine = reader.readLine()) != null)
{
    thisLine = thisLine.trim()
    // userData[0] => Username and userData[1] => Password
    String userData[] = thisLine.split(" ");
    if (user.displayUserName().equal(userData[0]))
    {
        System.out.print("\nUser <-" + user.displayUserName() + " -> exists!");
    }
}
相关问题