0

*Edit as I cannot use anything other than Scanner class.

I'm reading in a text file for a Conway's Game of Life program that looks like so:

------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
-------------X-X-X------------
--------------XXX-------------
-------------X-X-X------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------
------------------------------

I am trying to read all the characters into a single string variable and then parsing the string into a char array. How would I fix my code for the FileReader to read all the lines, and not stop after reaching the end of one?

    inputfile = JOptionPane.showInputDialog ("Where is the input file?   Ex:                   C:\\users\\public\\desktop\\input.txt ");
    Scanner input = new Scanner (new FileReader(inputfile)); 
    String values = null;
    while(input.hasNextLine()){
    values = input.next();
    }
    System.out.println(values);
  • Did you try printing the value of `input` - if you had, you would realize it was a short string, and not what you were thinking. Part of basic debugging techniques. Strongly suggest you learn to use a debugger so you can step through your code and inspect variables. Many problems like this one would be solved... – Floris Feb 18 '14 at 16:47

2 Answers2

3

input is path of the file (the user entered to the popup). Not the content of the file.

Here is a way to read a file line by line

Java read line from file

Community
  • 1
  • 1
  • I cannot use FileInputStream, etc..., as it was not part of the assignment. I decided to use the Scanner class to store the values into a string, but the FileReader stops inputting the values after the first line because of the break. Is there a way to tell FileReader to continue after the line ends? – user3038498 Feb 18 '14 at 17:19
  • You can read lines in a loop instead and append each line to a StringBuilder (currently you are replacing value of 'values' on each iteration. so you will only get the last line in this case). I assume your board size is constant. Also try to append your edits end of your question without deleting your code next time, or better create a new question with new code. Currently it looks like I am giving you a different answer –  Feb 19 '14 at 07:33
0

Please note that you are overwriting value in your loop every time (you do value = input.next();). I think you meant to add all lines together. After the loop is finished, the value String only contains the last line read, and as such that is the only line printed to System.out.

Chronio
  • 757
  • 3
  • 8