我目前正在开发一个解析CSV的程序,然后检查输入的电子邮件地址,如果有效则显示其人员信息。我遇到的问题是说你的电子邮件无效的其他声明。我目前的代码;
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语句完美无缺?任何帮助,将不胜感激。
答案 0 :(得分:1)
你不应该从循环中break
。您必须使用continue
,因为可能有其他行将通过电子邮件检查。我的意思是,因为第一行没有通过检查,这并不意味着第二行不会通过它。您必须检查所有行。
像这样:
else {
System.out.println("BROKEN");
dataRow = CSVFile.readLine();
continue;
}
或者只删除整个其他部分:
}
//here was the else section
System.out.println();
dataRow = CSVFile.readLine();