简体   繁体   English

while循环中的else语句损坏

[英]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. 我目前正在开发一个程序,用于解析CSV,然后对照CSV检查输入的电子邮件地址,如果有效,则显示其人员信息。 The problem I am coming across is the else statement to say that your email is invalid. 我遇到的问题是else陈述,说您的电子邮件无效。 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. 虽然如果我删除说if语句,则该程序可以完美运行,其余部分可以正常运行。 Have I somehow declared it in the incorrect place since the first else statement works perfectly? 自从else语句运行良好以来,我是否以某种方式在错误的位置声明了它? Any help would be appreciated. 任何帮助,将不胜感激。

You shouldn't break from the loop. 您不应该break循环。 You must use continue , because there can be other rows which will pass the email check. 您必须使用continue ,因为可能会有其他行通过电子邮件检查。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM