왜 if-else 블록이 감마가 null을 감지하지 못합니까? 여기 내 코드가있다. 스프레드 시트 응용 프로그램이며 테이블의 모든 셀을 반복하여 파일에 기록하는 저장 기능입니다. 여기 내 코드 :if if else is not가 제대로 작동합니까?
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()? –
@BrianAgnew 누군가가 여분의 유효 기간을 원할 수도 있기 때문에 모든 코드를 추가했습니다 –