帮助!代码不断返回为“nullnull”!我试图让它如此,如果我有多个元素,它会将它显示到控制台中。这是更多的两部分问题... 1.我将多个不同的元素符号保存到一个变量,它不会工作吗?我应该如何做到这一点,以便每次都保存在不同的变量下? 2.为什么它会继续返回nullnull?我认为东西应该被保存为元素1.不是很确定element11,但... 感谢您的帮助!java中的文件写入和读取始终返回为空
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import javax.swing.JOptionPane;
public class Science1 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String element;
int more;
FileInputStream fin;
JOptionPane.showMessageDialog(null, "Balancing chemical equations, capable for anything with more than one compound");
element = JOptionPane.showInputDialog("Please input an element");
String element1 = null;
element = element1;
String element11 = null;
//MORE VARIABLES?
more = JOptionPane.showConfirmDialog(null, "Do you have more elements on this side?", element, JOptionPane.YES_NO_OPTION);
while(more == 0){
element = JOptionPane.showInputDialog("Please input an element");
element = element11;
Writer writer = null;
try {
writer = new BufferedWriter(new FileWriter("test.txt"));
writer.write(element1 + element11);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) try { writer.close(); } catch (IOException ignore) {}
}
//MORE VARIABLES?
more = JOptionPane.showConfirmDialog(null, "Do you have more elements on this side?", element, JOptionPane.YES_NO_OPTION);
}
try
{
// Open an input stream
fin = new FileInputStream ("test.txt");
// Read a line of text
System.out.println(new DataInputStream(fin).readLine());
// Close our input stream
fin.close();
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
}
}