为什么我的if-else块没有检测到gamma是空的?这是我的代码。它是一个Spreadsheet应用程序,这是一个保存函数,循环遍历表中的所有单元格并将其写入文件。这里是我的代码:为什么我的工作不正常?
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 && !(gamma.isEmpty))
那么只有null写入文件作为伽马值。
也许,伽玛不是空的......但是空的,不是吗?检查如下:“gamma!= null &&!gamma.isEmpty()” – W0rmH0le
也许你可以减少你的代码示例到显着的行,并包括细节re。 Table.getValueAt()? –
@BrianAgnew我添加了所有的代码,以防万一有人想要额外的验证 –