1

I am trying to read a file into and array but it does not seem to working here is my code

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("Sales.txt"));
    int lineCount = 0;
    String line = "";
    while (input.hasNextLine()) {
        line = input.nextLine();   // read one line 
        lineCount++; //count line to find out how big array must be
        // System.out.println(line);
    }

    String[] fileinput = new String[lineCount]; //create array to store file in


    while (input.hasNextLine()) {
        int i = 0;
        fileinput[i] = input.nextLine();
        System.out.println(fileinput[i]);
        i++;

    }

I am using the first part to find out how many lines so I can specify my array to be that size then trying to put each line into the array in my second part.

But it keeps coming back as null each time it does not seem to want to read or work with the file a second time after it works with it the first time.

Any help would be greatly appreciated.

Thanks

Chris
  • 131
  • 2
  • 8

5 Answers5

3

You need to move the declaration of i outside the while loop and to restart to read the file from the beginning.

input = new Scanner(new File("Sales.txt"));
int i = 0,
while (input.hasNextLine()) 
        fileinput[i] = input.nextLine();
        System.out.println(fileinput[i]);
        i++;
}

You could also use a List to avoid iterating two times through your file and use readAllLines (if you're using Java 7).

File f = new File("Sales.txt");
Charset ch = //encoding of the file
List<String> allLines = Files.readAllLines(f.toPath(), ch);
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
3

You have to start the Scanner from the beginning for the second loop:

Add the following after String[] fileinput = new String[lineCount];

input = new Scanner(new File("Sales.txt"));

And you have to declare i outside of the second loop.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Abu Sulaiman
  • 1,477
  • 2
  • 18
  • 32
3

You need to initialize int i before while because every time it enters in loop it will initialized with 0 so there is no use of increment i.

while (input.hasNextLine()) {
    int i = 0;
    fileinput[i] = input.nextLine();
    System.out.println(fileinput[i]);
    i++;

}

modified your code with

int i = 0;
while (input.hasNextLine()) {
        fileinput[i] = input.nextLine();
        System.out.println(fileinput[i]);
        i++;
}
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
2

Here you go:

String[] fileinput = (new Scanner( new File("Sales.txt") ).useDelimiter("\\A").next()).split("[\\r\\n]+");

This is the one-liner that saves lines from file into an array of String.

Remember that you do not need to count the number of lines and all other deceleration of variables. i.e., your whole code can be replaced by only this one line.

Is this what you need?

tod
  • 1,539
  • 4
  • 17
  • 43
  • What about Strings with empty lines? This split will treat series of `\r\n\r\n...` as one token making `foo\r\n\r\nbar` same as `foo\r\nbar`. – Pshemo Apr 13 '14 at 11:55
  • @Pshemo I think this requirement also needs to be added into the question, otherwise this solution is good enough which i have been using and it is a complete answer to the question asked. if you do not mind, could you make the edit to mention these limitations in my answer. – tod Apr 13 '14 at 11:59
  • Thank you this is a bit beyond me at the moment as I am only in year 1 of java but it is nice to know it can be cut down. – Chris Apr 13 '14 at 12:00
  • @Chris You are welcome. No, it is not:) Just add this line into your IDE and run to see the result. I am also a beginner in Java. – tod Apr 13 '14 at 12:03
  • @Chris If you are looking for neat ways to read content of file take a look at `Files.readLines(path);` only problem for you now will be that this returns `List` of lines, not array, but you should get used to dynamic collections pretty soon. – Pshemo Apr 13 '14 at 12:05
  • @tod To solve problem from my first comment you can simply use `split(System.lineSeparator());` if line separator is related to OS, or by `split("\r\n?|\n");` this split can accept only `\r` `\r\n` or `\n` so it should be multi platform. – Pshemo Apr 13 '14 at 12:13
1

Easy way to do this is to read lines to List and then convert it to Array as done here Java: Reading a file into an array

Community
  • 1
  • 1
Hans
  • 752
  • 1
  • 15
  • 29