Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with arrays

Status
Not open for further replies.

RookThis

Technical User
Jul 27, 2002
195
US
Question:

If I have a file with 5 items per line, ex:

Car Red 4door Chevy Good
Truck Black Club-Cab Ford Fair
Car Grey 2door Honda Good
etc....

and I want to read those values into an array to be able to separate or tally how many I have per classification, how can I accomplish that?

I've tried reading them in but I keep getting a syntax error. Please advise


 
Why don't you post your trial code with the error message that you get. It is always better to learn from your own mistakes than letting other people do the job for 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];
while (ifile1.hasNext())
{
type = ifile1.next();
color = ifile1.nextInt();
description = ifile1.next();
make = ifile1.nextLine();
items[index].setType(type);
items[index].setColor(color);
items[index].setDescription(description);
items[index].setMake(make);
index++;
}
ifile1.close();

}
}
 
The code looks pretty good to me, with only two exceptions. 1. it tries to read color as an integer. To solve that you should replace "color = ifile1.nextInt();" by "color = ifile1.next();".
2. in your while statement you'd need to create the "items[index]" with an "items[index] = new carFile()", before you use it, otherwise a NullPointerException is thrown

If your textfile countaint more than car record, you'll have another problem (bounds violation). What I normally do is to read all record in a Vector) and afterwards copy the Vector into the array.

Something like this:

Scanner ifile1 = new Scanner(new File("input.txt"));
Vector<carFile> cars = new Vector<carFile>(50);
while (ifile1.hasNext()) {
carFile item = new carFile()
item.setType(ifile1.next());
item.setColor(ifile1.next());
item.setDescription(ifile1.next();
item.setMake(ifile1.nextLine());
cars.add(item);
}
ifile1.close();
carFile[] items = new carFile[cars.size());
cars.copyInto(items),


 
I tried your suggested code but I'm still getting errors. Any thoughts?

public class farm2
{
public static void main (String [] args)throws Exception
{
Scanner ifile1 = new Scanner(new File("cowBarn.txt"));
Vector<cowFile> cow = new Vector<cowFile>(50);
System.out.println ("Cows in 1323 Farm");
while(ifile1.hasNext())
{
cowFile item = new cowFile()
item.setName(ifile1.next());
item.setWeight(ifile1.next());
item.setGender(ifile1.next());
item.setBreed(ifile1.nextLine());
cow.add(item);
}
ifile1.close();
cowFile[]items = new cowFile[cow.size()];
cow.copyInto(items);
 
the line "cowFile item = new cowFile()" should have an ";" at the end. Could you post the error-message(s) that you get.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top