【问题标题】:Why does my if else not function properly? [duplicate]为什么我的 if else 不能正常工作? [复制]
【发布时间】:2016-07-06 16:11:55
【问题描述】:

为什么我的 if-else 块没有检测到 gamma 为空?这是我的代码。它是一个电子表格应用程序,这是一个保存函数,它遍历表中的所有单元格并将其写入文件。这是我的代码:

public void Save () {
        String FileName = JOptionPane.showInputDialog("Please enter the name for your file:");

        if (FileName == null) {
            System.err.println("You didn't enter anything");
        } else {
            int rows = Table.getRowCount();
            int columns = Table.getColumnCount();
            int alpha = 0;
            int beta = 1; 
            choicer.setCurrentDirectory(new java.io.File("Desktop"));
            choicer.setDialogTitle("Save Document");
            choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            choicer.setAcceptAllFileFilterUsed(false);
            if (choicer.showSaveDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
                dir = String.valueOf(choicer.getSelectedFile());
            }
            File f = new File(dir + "\\" + FileName + ".txt");
            try {
                fos = new FileOutputStream(f);
                osw = new OutputStreamWriter(fos);
                w = new BufferedWriter(osw);
                for (alpha = 0; alpha <= rows - 1; alpha++) {
                    for (beta = 1; beta < columns; beta++) {
                        String gamma = String.valueOf(Table.getValueAt(alpha, beta));
                        if (gamma != null) {
                            w.write("<%! " + gamma + " !%> ");
                        } else {
                            w.write(" <%! |^-^| !%> ");
                        }                           
                    }
                    w.write("\n");
                }
            } catch (IOException e) {
                System.err.println("Some prob dude. Too big for me"); 
            } finally {
                try {
                    w.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

我尝试了重复的东西给出的链接,但这并没有解决我的问题。如果我这样做if(gamma != null &amp;&amp; !(gamma.isEmpty)),那么只有 null 作为 gamma 的值写入文件。

【问题讨论】:

  • 也许,gamma 不是空的……而是空的,不是吗?检查如下:“gamma != null && !gamma.isEmpty()”
  • 也许您可以将代码示例减少到显着的行,并包含详细信息。 Table.getValueAt() ?
  • @BrianAgnew 我添加了所有代码以防万一有人需要额外的验证
  • @GuilhermeP 如果我这样做 if( gamma != "") 它会写 null
  • @RahulSrinath 这是因为在您调用 String.valueOf 之前的行上的变量值为 nullgamma 在这种情况下不是null,而是变成了字符串"null"

标签: java file


【解决方案1】:

也许,gamma 为空但不为空:

改变

if (gamma != null)

if (gamma != null && !gamma.isEmpty())

【讨论】:

  • 如果 gamma == " ",它不被认为是空的,对吧?
  • 嗯。我需要检查...最初。我相信空格不是空字符串
  • isEmpty 的文档表明它基本上是检查字符串长度是否等于零的简写 - 所以不,由单个空格组成的字符串不会被视为空,因为它们有非零长度。
  • @JonK 感谢您澄清这一点!谢谢
  • @GuilhermeP 现在一切都被认为是空的
【解决方案2】:

我能想到的只有三件事会导致这种情况。我会在一秒钟内找到那些。首先,您使用的是什么 IDE?你有能力设置断点和观察变量吗?这将帮助您解决以下问题。 1.你能保证第一个for循环命中吗? 2. 然后,确保进入第二个 for 循环。 3. 最后,字符串 gamma 的实际值是多少?这是您需要一个断点并在您期望它为空时检查该变量的地方。可能你会看到它不是。当然,前提是你的 for 循环甚至可以让你到达那里。

希望这会有所帮助!

【讨论】:

  • 我也同意@Guilherme P 的观点。这也很可能是原因。
  • 我的循环工作并且非常好
猜你喜欢
  • 2021-01-14
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多