Incrementing an Array element

A

Andy K

I am tryign to read values from an array but at the moment I am only
able to read one element a. if I set i=0 it will read onle that
element. However if I set it to 6 or any other number it wont read. I
want to be able to read this array in reverse order. What is wrong with
my for loop or the whole code.

// create array of object type Premiership with 20 elements
Premiership a[] = new Premiership[20];
int i = 0;

StringTokenizer tokens;
// this is needed to be able to split a string object into separate
components

tokens = new StringTokenizer(textLine); // to split the read string
into tokens
teamName = tokens.nextToken();

// the team name component extracted from the string
points = Integer.parseInt(tokens.nextToken());

// the points component extracted from the string

a = new Premiership(teamName,points);

for (i=0; i<=19; i++)
{
System.out.println(a.getTeamName() + " " + a.getPoints());
System.out.println(a[8]);
}

textLine = fileInput.readLine(); // Unicode format string
moreToRead = (textLine != null);


} // end of while (moreToRead)
 
S

Sergio Juan

Premiership a[] = new Premiership[20];
int i = 0;
a = new Premiership(teamName,points);


You only put an object in the first position of the array. The rest of the
array has space allocated, but its value is undefined or NULL (I don't
remember now) and you can access to the methods of a NULL Object. Try to
put something in there before getting it; java is good but still needs you
to populate the lists...

HTH
 
A

Andy K

I have tried this man but its only readign the first element and stop
there. I cant understand because its in a loop. Help

Sergio said:
Premiership a[] = new Premiership[20];
int i = 0;
a = new Premiership(teamName,points);



You only put an object in the first position of the array. The rest of the
array has space allocated, but its value is undefined or NULL (I don't
remember now) and you can access to the methods of a NULL Object. Try to
put something in there before getting it; java is good but still needs you
to populate the lists...

HTH
 
M

Michael Borgwardt

Andy said:
I have tried this man but its only readign the first element and stop
there. I cant understand because its in a loop. Help

No, it's NOT in a loop.
 
A

Andrew Thompson

Andy K said:
I am tryign to read values from an array but at the moment I am only
able to read one element a.


Please do not post 'code snippets' Andy -
they are about as useful as breasts on a bull.

Cut your problem down to a 20-30 line program
that displays the problem and 'copy/paste' that
program to your post.

Cutting code that fails down to the bare minimum
often helps _you_ find the mistake, and allows us
to see the mistake more quickly and easily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top