Use ArrayList class. It's size (length) grows automatically when you add an element.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
Hi
You say that you want to read the eight first line from my text file but
your while loop is set to continue as long as the lines to read is not null. Your int[]var on the other hand has the length of 8 which means that you will get a ArrayIndexOutOfBoundsException when you come to line number nine in your text file. Maybe you should set a condition that stops either when the array is filled or the lines to read are null.
You are aware of ( I hope) that your int[]var only can accept strings that can be converted to numbers by Integer.parseInt(ligne.substring(0, ind), like 1, 2, 3. So if you have any other chars like a, b, c in your text you will get a NumberFormat error. What you catch is the first "word" in the line because
int ind = ligne.indexOf(" ") counts the chars until the first empty space " " appears.
Both your while loop and for loop works fine according to the conditions you have set up, if you test them one by one.
Think about this...
Does the while loop work as you want it to do?
Is an int array the best to use?
Has your array so many places it needs in this program?
Do you get the output you want in ligne.indexOf(" ")?
Has the text only numbers?
Good luck =)
sneaker
Junior Poster in Training
77 posts since Jul 2009
Reputation Points: 40
Solved Threads: 13