【发布时间】:2013-11-24 16:06:52
【问题描述】:
帮助!代码一直返回为“nullnull”!我正在努力做到这一点,如果我有多个元素,它将显示在控制台中。这更像是一个两部分的问题...... 1. 我将多个不同的元素符号保存到一个变量中,它不会起作用吗?我应该如何使它每次都保存在不同的变量下? 2. 为什么一直返回nullnull?我认为应该将某些内容保存为元素 1。虽然对 element11 不太确定... 感谢您的帮助!
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);
}
}
}
【问题讨论】:
标签: java filereader filewriter