Cooke1007
Cooke1007

Reputation: 73

Broken else statement within a while loop

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 = "[email protected]";
    //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.

Upvotes: 0

Views: 196

Answers (1)

Petar Minchev
Petar Minchev

Reputation: 47373

You shouldn't break from the loop. You must use continue, 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:

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

Or just remove the whole else section like this:

}

//here was the else section

System.out.println();
dataRow = CSVFile.readLine();

Upvotes: 1

Related Questions