0

我目前正在开发一个程序来解析 CSV,然后根据 CSV 检查输入的电子邮件地址,如果它有效,则显示他们的人员信息。我遇到的问题是 else 声明说您的电子邮件无效。我当前的代码;

import java.io.*;
import java.util.*;

public class CSVReader123{

public static void main(String[] arg) throws Exception {

    BufferedReader CSVFile = new BufferedReader(new FileReader("testa453.csv"));
    String dataRow = CSVFile.readLine();
    //String email = "ojones@coldmail.net";
    //String password = "ocrabc";

    Scanner input = new Scanner(System.in);
    System.out.println("Please input an email adress");
    String email = input.nextLine();
    System.out.println("Please input you password");
    String password = input.nextLine();

    while (dataRow != null){

        String[] dataArray = dataRow.split(",");
        if ((dataArray[0].equals(email)) && (dataArray[1].equals(password))) {
            System.out.println("Your email is valid");
            System.out.println("Do you want to display you personel data?(Y or N)");
            String personeldata = input.nextLine();
            if ((personeldata.equals("yes")) || (personeldata.equals("Yes")) || (personeldata.equals("Y"))){
                System.out.println("You email is " +dataArray[0]+".");
                System.out.println("You password is " +dataArray[1]+".");
                System.out.println("You first name is " +dataArray[2]+".");
                System.out.println("You second name is " +dataArray[3]+".");
                System.out.println("You street name is " +dataArray[4]+".");
                System.out.println("You city name is " +dataArray[5]+".");
                System.out.println("You postcode is " +dataArray[6]+".");

            }
            else if ((personeldata.equals("no")) || (personeldata.equals("No")) || (personeldata.equals("N"))) {
                System.out.println("Shutting down.");
                break;
            }

        }
        else {
            System.out.println("BROKEN");
            break;
        }
        System.out.println();
        dataRow = CSVFile.readLine();
    }
    CSVFile.close();
    System.out.println();
}
}

我遇到的问题是,如果我尝试输入有效或无效的电子邮件,它总是会打印出损坏并停止。虽然如果我删除所说的 if 语句,程序运行完美,其余部分正常工作。自从第一个 else 语句完美运行以来,我是否以某种方式在不正确的位置声明了它?任何帮助,将不胜感激。

4

1 回答 1

1

你不应该break从循环中。您必须使用continue,因为可能还有其他行将通过电子邮件检查。我的意思是,因为第一行没有通过检查,并不意味着第二行不会通过它。您必须检查所有行。

让它像这样:

else {
     System.out.println("BROKEN");
     dataRow = CSVFile.readLine();
     continue;
}

或者像这样删除整个 else 部分:

}

//here was the else section

System.out.println();
dataRow = CSVFile.readLine();
于 2012-11-17T13:06:57.780 回答