-1

Ok, so I'm having trouble figuring out how to assign numbers from a txt file to a String variable. My implementation ALMOST does what I want but its not quite right. With my code, it takes a line in the txt file and re-assigns it every time the loop restarts. Meaning by the time it outputs, the only thing shown is the very last line of the file. I would like it to contain the entirety of the txt file.

P.S.) I dont actually need to printout the variable, I only did it to verify whether or not it contains everything I need. I'm working on a much larger project and if I can get this working I should be able to adjust my actual project accordingly. Also, I would really appreciate it if any solution/help given doesn't try to make me use an array. If I have to turn the variable into an array then I would have to re-code everything else to fit it.

public class test {
    public static String num;
    public static void main(String[] args) {

        BufferedReader reader = null;

        try {
            File file = new File("test3_14.txt");
            reader = new BufferedReader(new FileReader(file));

            String line;
            while ((line = reader.readLine()) != null) {
                //System.out.println(line);

                num = line;
                //System.out.println(num);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.print(num);
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Sir Brill
  • 1
  • 1
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help) , in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). – Timothy Truckle Nov 07 '17 at 18:20
  • 1
    Possible duplicate of [How do I create a Java string from the contents of a file?](https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) – Timothy Truckle Nov 07 '17 at 18:21

4 Answers4

0

I assume you want to concatenate all the numbers into a single String

Create a StringBuilder object

StringBuilder stringBuilder = new StringBuilder();

Append lines to it:

stringBuilder.append(line);
stringBuilder.append(","); // if you want to separate the numbers

After the loop finishes, build the String and save it in your varible

num = stringBuilder.toString();
kozeljko
  • 160
  • 2
  • 13
0

did you try changing num = line; to num += line;?

stackguy
  • 188
  • 5
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/17869745) – Justin Pearce Nov 07 '17 at 19:03
  • I ran the program that was posted in the questions, and `num += line;' was only change needed to arrive at the answer OP was looking for. @JustinPearce ? – stackguy Nov 07 '17 at 21:27
0

It can be seen from your code that you are not appending the new line into the text. You are overriding it each time. You can follow any of the below approaches -

  • instead of using (=), use (+=). The better approach would be to use Stringbuilder and later converting to string.

  • you can use FileUtils.readFileToString(File, Charset). This method reads contents of a file to string.

  • If you are using Java 8, you can use Files.lines() method to get Stream.

ash164
  • 231
  • 3
  • 9
0

It can be seen from your code that you are not appending the line into the text. You are overriding it each time. You can follow any of the below approaches -

  • instead of using (=), use (+=). The better approach would be to use Stringbuilder and later converting it to string.

  • you can use FileUtils.readFileToString(File, Charset). This method allows you to read the entire file as string.

  • if you are using Java 8, you can also use Files.lines() to get Stream.

ash164
  • 231
  • 3
  • 9