1

I've got a bit of a problem with my java code. I am trying to create an object array that allows a line of input from a text file.

This is my code:

code withheld, solution found

That obviously initialises an object array with the size returned from the method.

I try to make a loop that reads each line from the file 'songFile' but returns an error.

Here is my code for the loop:

code withheld, solution found

This is the code for the getLineAmt method:

code withheld, solution found
6
  • 1
    What error does it "return"? Commented Oct 22, 2013 at 4:18
  • Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at mainProgram.main(mainProgram.java:23) Commented Oct 22, 2013 at 4:20
  • line 23 is the code inside the for loop Commented Oct 22, 2013 at 4:21
  • Show us how you initialize songFile?? Commented Oct 22, 2013 at 4:22
  • Can you post the code for your getLineAmt() method? Commented Oct 22, 2013 at 4:24

3 Answers 3

1

Your getLineAmt(Scanner songFile) method is consuming your file when counting the number of lines. Once the method returns, your Scanner has arleady read the file and as reached the end.

One way to solve that is to simply use a List object instead of an array, so that you do not need to know the size or your file in advance.

List<Song> songList = new ArrayList<Song>();

while(songFile.hasNextLine()) {
    songList.add(new Song(songFile.nextLine()));
}
Sign up to request clarification or add additional context in comments.

7 Comments

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Song.<init>(Song.java:17) at mainProgram.main(mainProgram.java:22)
This looks like a problem in your Song constructor. Have you tested the Song class on it's own to make sure it works?
I have, I've made a single 'Song' object, and have used just the first line. The constructor is a string. The string is several characteristics that are split by a comma, and I've used a ".split(",")" function to get those variables. seeing the error, it seems as though my last split is what is causing the error.....
Then you may need to validate the data in your "songCollection.txt" file. Or try running the program in a debugger to see what is going on. Without the code to Song.java and without the data file, it is hard to say more.
Thank you for your help Francis. Removing the last "split" seemed to fix the problem. I will try to make a work-around for this. :) The ArrayList works, however every single item in the array seems to be data from the last line.... any way to fix this?
|
0

You are getting this error because you are trying to read a line without making sure if there are any more lines to be read. You need to modify and add the following check before reading the nextLine

for (int i = 0; i < songArray.length; i++)
{
if(songFile.hasNext()){
songArray[i] = new Song(songFile.nextLine());
}
}

1 Comment

this fixes the error, but when trying to read a variable from i.e. songArray[1], it just shows null. same for any other number.
0

You're counting the lines in the song file, so you are at end of stream, so when you call nextLine() there isn't one. Use an ArrayList<Song> instead of an array of Song[], then you don't need to count the lines at all.

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.