2

I have a .txt file with, for example, this content:

variable1="hello";
variable2="bye";
testing3="parameter";
whatisthis4="hello";
var5="exampletext";
example=3;
wellthen=8;
---

It read in the file, line by line, fine until I added a way of saving the data.

This whole code plus another reader (with other variable names of course) is wrapped in a try-catch statement.

String path_playlist = new File("").getAbsolutePath();
String fileName_playlist = path_playlist 
        + "/src/dancefusion/game/playlist.txt";
FileReader fr_playlist = new FileReader(fileName_playlist);
BufferedReader br_playlist = new BufferedReader(fr_playlist);

int track_counter = track_sum*9;
String trackinfos[] = new String[track_counter];

while(track_counter < 0)
{
    System.out.println("linecount="+track_counter);
    trackinfos[track_counter] = br_playlist.readLine();
    System.out.println(trackinfos[track_counter]);
    track_counter--;
}
System.out.println(Arrays.toString(trackinfos));

In this example track_sum equals 1.

The while loop should read in the file one line at a time but only reads null's:

[null, null, null, null, null, null, null, null, null]

Update 1:

The while-condition was set up the wrong way... thanks!

The corrected version:

while(track_counter < 0)

However, now it gives me an exception with an "ArrayOutOfBounds: 9".

Any guesses?


Final Update:

As mentioned by @GiorgiMoniava, I just needed to reduce track_counter by one before starting to read in as in Java arrays begin with 0, thanks!

int track_counter = track_sum*8;
String trackinfos[] = new String[track_counter];

track_counter--;

while(track_counter >= 0)
{
    System.out.println("linecount="+track_counter);
    trackinfos[track_counter] = br_playlist.readLine();
    System.out.println(trackinfos[track_counter]);
    track_counter--;
}

Maybe one of you can figure out what I did wrong...

Of course I can deliver more information/code if needed! Thanks in advance!

1
  • 1
    maybe a typo? while(track_counter > 0) Commented Jun 30, 2016 at 18:42

2 Answers 2

3

This looks weird

while(track_counter < 0)

Are you sure loop is ever entered in? It is my guess (from your output) that track_counter is 9.

About your array out of bounds exception: if you create array of size N you can only access it using indexes: [0, N-1]

Sign up to request clarification or add additional context in comments.

8 Comments

it doesnt make sense since he is also referencing an array index with this track_counter which, if entered, will be < 0. but i dont think it enters that part
@Apostolos I also don't think loop is entered in; ps. not a Java expert, just a guess
Thanks, this solved the first part - now it seems that it enters the loop - however I am getting a ArrayIndexOutOfBoundsException: 9 Any idea how to get rid of that too? Thanks!
@KaiAdam Doesn't matter point is if you have array with size 500 you can only access elements at index 0, ..., 499
@KaiAdam Because you probably are still trying to access last element via [500] which is out of bounds (which are 0-499 for that case). Problem is not in array size, but in way you are trying to access its elements.
|
1

try this

   while(track_counter > 0)
    {
        System.out.println("linecount="+track_counter);
        track_counter--;
        trackinfos[track_counter] = br_playlist.readLine();
        System.out.println(trackinfos[track_counter]);

    }

3 Comments

Thanks for helping me out - however, @GiorigeMoniava got it first :S
ok this doenst stop you from upvoting the answer for being helpful. you can accept giorrgi's answer of course :)
I upvoted both answers of course :) Sadly it says that my votes won't be visible as I am still below 15 reputation points :S

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.