I am currently been working on a program to parse CSV’s and then check an inputted email adress against the CSV and if it is valid display their personnel information. The problem I am coming across is the else statement to say that your email is invalid. My current code;
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();
}
}
The problem which I am having is that if I try to enter a valid or invalid email it will always print out broken and stop. Although if I remove said if statement the program runs perfectly and the rest of it works correctly. Have I somehow declared it in the incorrect place since the first else statement works perfectly? Any help would be appreciated.
You shouldn’t
breakfrom the loop. You must usecontinue, because there can be other rows which will pass the email check. I mean, because the first row does not pass the check, it doesn’t mean that the second row won’t pass it. You must check all rows.Make it like this:
Or just remove the whole else section like this: