【问题标题】:if else statement not stopping issueif else 语句没有停止问题
【发布时间】:2016-12-12 03:31:35
【问题描述】:

我有一个代码,它有一个按钮,可以获取文本字段的输入,然后查看 .txt 文件,看看是否有匹配项。唯一的问题是它遍历每一行并检查该行是否匹配,这意味着 ifelse 语句都会被触发。所以让我们说 .txt 文件有 3 行说 123454238543 然后输入将是 1234,然后代码按照 if 语句的预期运行,因为输入匹配,这是应该的。但随后它继续运行,因此它也触发了它不应该触发的 else 语句。

这是我的代码

public static String input = "";

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

        String input = jTextField1.getText();

        File file =new File("file.txt");
        Scanner in = null;
        try {
            in = new Scanner(file);
            while(in.hasNext())
            {
                String line=in.nextLine();
                if(line.contains(input)){

                    popUp1 pu1 = new popUp1();
                    pu1.setVisible(true);

                } else {

                    popUp2 pu2 = new popUp2();
                    pu2.setVisible(true);

                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("Error");
        }

    }

【问题讨论】:

  • 当 if 语句发生时你想break while 循环吗?
  • 如果不是,请澄清“它不应该是什么”,因为那是你编写代码要做的事情

标签: java file if-statement methods java.util.scanner


【解决方案1】:

你的方法是错误的。如您所见,如果文件中的行与输入匹配,则会触发“失败”处理。相反,您需要检查整个文件,检查匹配的行,如果找到则触发该行为,然后终止循环。只有当您完成文件但仍未找到匹配项时,您才应该触发“失败”行为:

boolean found = false;
while(!found && in.hasNext()) {
    String line = in.nextLine();
    if (line.contains(input)) {
        found = true;
        popUp1 pu1 = new popUp1();
        pu1.setVisible(true);
    }
}

// If we've gone over the entire file and still not found anything:
if (!found) {
    popUp2 pu2 = new popUp2();
    pu2.setVisible(true);
}

【讨论】:

    【解决方案2】:

    假设如果文件包含值,则需要 popup1,否则需要 popup2。

    请注意,我忽略了 pu1pu2 变量的范围问题以及我们在 EDT 中有文件 IO 的事实。

    boolean doPopupOne = false;
    while(in.hasNext())
    {
        String line=in.nextLine();
        if(line.contains(input)){
            doPopupOne = true;
            break;
        } 
    }
    if (doPopupOne)
    {
        popUp1 pu1 = new popUp1();
        pu1.setVisible(true);
    }
    else
    {
        popUp2 pu2 = new popUp2();
        pu2.setVisible(true);
    }
    

    【讨论】:

      【解决方案3】:

      我想我明白你想要做什么:)。如果在 .txt 文件中找到或没有,您只想显示一条消息。添加一个计数器来记录它在 .txt 文件中出现的次数。如果您想在找到第一个匹配项时停止,只需插入一个中断;在 if 子句中,将 else 移到 while 子句之后,您可以在其中检查计数器,如下所示:

      Scanner in = null;
      int count = 0;
      try {
          in = new Scanner(file);
          while(in.hasNext())
          {
              String line=in.nextLine();
              if(line.contains(input)){
                  count++; 
                  popUp1 pu1 = new popUp1();
                  pu1.setVisible(true);
                  break;
              }
          }
          if (count == 0){
              popUp2 pu2 = new popUp2();
              pu2.setVisible(true);
          }
      } catch (FileNotFoundException e) {
          System.out.println("Error");
      }
      

      如果您对它在文件中出现的次数感兴趣,请将 if 移到 while 子句之后,与 pu2 的情况相同:

      Scanner in = null;
      int count = 0;
      try {
          in = new Scanner(file);
          while(in.hasNext())
          {
              String line=in.nextLine();
              if(line.contains(input)){
                  count++; 
              }
          }
          if (count > 0){
              // count contains the number of matches
              popUp1 pu1 = new popUp1();
              pu1.setVisible(true);            
          }
          else {
              popUp2 pu2 = new popUp2();
              pu2.setVisible(true);
          }
      } catch (FileNotFoundException e) {
          System.out.println("Error");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        • 2017-02-11
        • 1970-01-01
        • 1970-01-01
        • 2013-12-07
        • 2014-09-26
        • 1970-01-01
        相关资源
        最近更新 更多