0

I am trying to take in user input and storing it in a text file, I was able to take in the input and storing it but now i am trying to display that input in a JOptionPane window. Can someone please help me. I am new on stackflow and this is my first post.

/*Here i am taking in user input and storing it in a file called "dictionary.txt".*/
public static String addNewWord()throws IOException
{
    String newWord = "";

    newWord = JOptionPane.showInputDialog(null, "Enter the word you want to add to your dictionary.");

    FileWriter aDictionary = new FileWriter("dictionary.txt", true);
    PrintWriter out = new PrintWriter(aDictionary);
    out.println(newWord);
    out.close();
    aDictionary.close();

    return newWord;
}
/* Now i am trying to read "dictionary.txt" and display it using JOptionPane*/
public static String listDictionary()throws IOException
{
   FileReader aDictionary = new FileReader("dictionary.txt");
   String aLineFromFile = FileReader;
   JOptionPane.showMessageDialog(null, aLineFromFile);
   aDictionary.close();

   return aLineFromFile;
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
David Nkanga
  • 1
  • 1
  • 3

1 Answers1

0

You should use a BufferedReader to read the data back from the file:

public static void listDictionary()throws IOException
{
    BufferedReader br = new BufferedReader(new FileReader("dictionary.txt"));
    String aLineFromFile = null;
    while ((aLineFromFile = br.readLine()) != null){
            JOptionPane.showMessageDialog(null, aLineFromFile);
    }        
    br.close();
    return;
}
redDevil
  • 1,909
  • 17
  • 25
  • thanks, and if its multiple lines how would i do that? currently it only reads one line of the text file. – David Nkanga Mar 31 '13 at 07:51
  • i have updated the code to reflect reading multiple lines, and incidently I don't think you want to return a string from this function. I have removed that as well – redDevil Apr 01 '13 at 08:51