How can I remove the random texts that shows up in my textfile?

23 Views Asked by At

Summary: I used JOptionPane to ask the user a question about their dental problems. I used an ArrayList to read the options they chose and used a class object to return the complimentary treatment as an ArrayList. I am trying to output the ArrayList containing the preferred treatments, but random text shows before the text that is needed.

This is the code used to write to a file:

//First parameter is the arraylist containing //the options the user chose, the latter is the class object that converts from int to string arraylist and //returns recommended treatment
public static void saveInfo(ArrayList<Integer> s, Symptoms es)
    //First parameter is the arraylist containing //the options the user chose, the latter is the class object that converts from int to string arraylist and //returns recommended treatment
    {
        
        String txtfile = ".txt";
        String filename = JOptionPane.showInputDialog(null, """
                                            Enter the name of the file you wish
                                            to save this information in. We will 
                                            create a text file you may refer to.
                                            """);
        filename+=txtfile;
        
        try{
            
            FileOutputStream writeData = new FileOutputStream(filename);
            ObjectOutputStream writeStream = new ObjectOutputStream(writeData);
            
            System.out.println(es.getTreatment(s));
            

            writeStream.writeObject(es.getTreatment(s));
            writeStream.flush();
            writeStream.close();
            
            JOptionPane.showMessageDialog(null, "Data written to file");
        
        
        }catch(Exception ex)
        {
            System.out.println("Error is " + ex.toString());
        }
    }

This is the output I get on the cout screen:

[1, 4]//for test run purposes, please ignore [You need a tissue graft, You need an implant or bone graft]//output that I want to write to file

What I get in the textfile:

¬í sr java.util.ArrayListxÒ™Ça I sizexp w t You need a tissue graftt !You need an implant or bone graftx

I tried using the File and FileWriter class to write to the file, but I could not find any way to output the arraylist string. I tried to convert the arraylist string to a string array, but I did not see that working.

I would like to format the output so that it produces:

You need a tissue graft\n You need an implant or bone graft

I was unable to find any resources to create that output on the text file.

0

There are 0 best solutions below