0
try {                    
    String fileName = " ";
    // FileReader reads text files in the default encoding.
    FileReader reader = new FileReader(fileName);

    // read as strings
    Scanner in = new Scanner(reader);               
    String finalValues = "";

    while (in.hasNextLine()) {
        finalValues = in.nextLine();

        txtArea.setText(finalValues);                   
        System.out.println(finalValues);
    }                  
    reader.close();

} catch (IOException e) {
    e.printStackTrace();                                    
}  

The code above is to read all the values from a file to a text area. Sorry I did not put the actual file name there. I'm able to use System.out.println() to print out all the values. But every time I try to set the values to the text area, only the last value of the file is displayed in the text area.

What am I missing?

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
ranaz
  • 97
  • 1
  • 10

3 Answers3

3

Here:

txtArea.setText(finalValues);     

that sets the complete text of the area. Erasing whatever you stored there with previous calls.

You have to collect your input (for example using a StringBuilder and then when all lines have been read, you call setText(builder.toString()) once.

More tips: there is Files.readAllLines() that you could use. And try-with-resources as well.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

GhostCat's answer is the best. Use StringBuilder because it uses less memory (When to use StringBuilder in Java). Or you can just use += instead of =,

finalValues += in.nextLine();
  • = assigns a value.
  • += appends a value (combine the value to the existing value).

while (in.hasNextLine()) {
    finalValues += in.nextLine();
}
txtArea.setText(finalValues); 
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
  • 1
    good explanation. Just a small comment on the wording *appends*. `a+=b` is short for `a = a+b`. But `Strings` are inmutable, meaning that `+=` does not "append" but rather it concatinates the two strings and writes the result in a new String (the previous value of `a` gets overwritten by the result of the concatination). That's basically the reason why `StringBuilder` is recommended here. Rule of thumb: when you have more than 3 String concatinations, use StringBuilder, for less you can stick to the `+` operator (for readability) – GameDroids Sep 20 '18 at 09:18
0

You were previously setting the new text over the old one, so only the last one remained, try this:

try {
    String fileName = " ";
    // FileReader reads text files in the default encoding.
    FileReader reader = new FileReader(fileName);

    // read as strings
    Scanner in = new Scanner(reader);
    String finalValues = "";

    while (in.hasNextLine()) {
        finalValues += in.nextLine();
        System.out.println(in.nextLine());
    }
    txtArea.setText(finalValues);
    reader.close();

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
Enashgrow
  • 71
  • 5
  • That is a really inefficient way to build a string in a loop. There is a reason why I suggested to use a StringBuilder instead. – GhostCat Sep 20 '18 at 07:55
  • The only reason i used this approach, was that it was the way he felt comfortable with, since it was the one he was using. – Enashgrow Sep 20 '18 at 07:56
  • keep in mind that we write answers not only for the OP, but all future readers. Beyond that: it is always a good idea to properly format source code. Dont just copy the lazy input the OP used, but show everybody that you can do better – GhostCat Sep 20 '18 at 08:00