-1

Help! The code keeps returning as "nullnull"! I am trying to make it so that if I have multiple elements, it will display it into the console. This is more of a two part question... 1. I am saving multiple different element symbols to one variable, it wont work will it? How should I make it so that it saves under a different variable each time? 2. Why does it keep returning as nullnull? I think something should be being saved as element 1. Not so sure about element11 though... Thanks for the help!

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);
        }
    }
}

3 Answers3

0
  1. After reading the input to element, you have assigned it to a variable element1 which is null.

    element = JOptionPane.showInputDialog("Please input an element");
    String element1 = null;  //<<-----remove
    element = element1;   //<<-----remove
    
  2. Initialize you BufferedWriter instance outside of the while loop.

  3. If you are using java 7, then use try-with-resource statement which will automatically close the resource for you.

Sage
  • 15,290
  • 3
  • 33
  • 38
0

replace

element = element1;

with

element1 = element;

and

     element = element11;

with

     element11 = element;

then append each new word to the text file

try {
    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("test.txt", true)));
    writer.println( element11);
    writer.close();
    } catch (IOException e) {
     //!
    }
iShaalan
  • 799
  • 2
  • 10
  • 30
0

You have, in essence, this:

    String element1 = null;
    String element11 = null;

    while(more == 0){
        try {
            writer = new BufferedWriter(new FileWriter("test.txt"));
            writer.write(element1 + element11);

But you're setting element1 and element11 nowhere. What else than "nullnull" should be the result?

Ingo
  • 36,037
  • 5
  • 53
  • 100