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

0

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi ZouZou thanks I just tried it though and it still does not seem to work. Still all cells in the array show as null.
@Chris You need to reset the scanner too.
Ahh that was it, I did not realise I needed to reset the scanner each time it was the cause of all my problems :)
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.

2 Comments

ZouZou, I didn't see your answer before I answered. Sorry.
Hi halal48, Thank you for the help bot you and ZouZou helped me find the answer I needed to reset the scanner each time :)
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++;
}

1 Comment

Thank you I was indeed missing this info :)
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?

6 Comments

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 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.
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 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.
@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.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.