-1

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

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

用户密码克里斯密码米歇尔密码约翰密码

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

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

static void readFromCsvFile(String sFileName, User user) throws 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();
        } 
4

2 回答 2

2

这里有几点说明:

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

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

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

while4)由于您在不需要内部的条件下读取新行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 */ }
    }
}
于 2012-10-05T13:17:43.277 回答
1

比较两个字符串:

firstString.equal(secondString);

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

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!");
    }
}
于 2012-10-05T13:07:49.650 回答