私のif-elseブロックがガンマがヌルであることを検出しないのはなぜですか?ここに私のコードです。これはスプレッドシートアプリケーションであり、これはテーブル内のすべてのセルをループしてファイルに書き出す保存関数です。ここに私のコードです:なぜ私のif elseは正しく機能しないのですか?
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のみがガンマの値としてファイルに書き込まれます。
おそらく、ガンマはnullではありませんが空ではありませんか?次のようにチェックします。 "gamma!= null &&!gamma.isEmpty()" – W0rmH0le
おそらくコードサンプルを顕著な行に減らし、詳細を追加することができます。 Table.getValueAt()? –
私はそれがオンラインにあなたが 'String.valueOf'に呼び出す前にあるためであるヌル –