Reading a file in

Hi

I am trying to read in a file to the Grocery store class as an array ( Item [] items = new Item[20];) then pass some of the generic attributes into the Item class and and then pass those attributes to the Fruit and Veg Class

Then in the same application after altering the data in the application i am going to write the altered data out to the same file replacing what was there previously.

The code i have is

Master Application class

class GroceryStore

{

privatestaticfinal Scanner console =new Scanner(System.in);

privatestaticfinal Item [] items =new Item[20];

privatestaticint numberOfRecords = 0;

publicstaticvoid main (String [] args)

{

publicstaticvoid loadData()

{

int index = 0;

Item temp =null;

try

{

BufferedReader br =new BufferedReader (new

FileReader ("grocery.dat"));

String inputLine;

inputLine = br.readLine();

while (inputLine !=null)

{

if (inputLine.equals("Fruit"))

{

temp =new Fruit();

}

elseif (inputLine.equals("Vegetable"))

{

temp =new Vegetable();

}

if (temp !=null)

{

temp.readAttributes(br);

items[index] = temp;

index ++;

inputLine = br.readLine();

temp =null;

}

br.close();

}

}

catch (FileNotFoundException e)

{

System.out.println("File grocery.dat was not found");

System.out.println("Error:");

System.out.println(" ");

System.err.print(e);

System.exit(1);

}

catch (IOException e)

{

System.out.println("there was an error reading the file");

}

}

Then in the Item Class i have:

import java.io.*;

publicabstractclass Item

{

public String itemCode;

public String type;

public String variety;

public Item()

{

itemCode ="Not Specified";

type="None";

variety ="None";

}

public Item(String itemCode, String type, String variety)

{

this.itemCode = itemCode;

this.type = type;

this.variety = variety;

}

public String getItemCode()

{

return itemCode;

}

public String getType()

{

return type;

}

public String getVariety()

{

return variety;

}

publicvoid readAttributes(BufferedReader br)throws IOException

{

this.itemCode = br.readLine();

this.type = br.readLine();

this.variety = br.readLine();

}

publicvoid writeAttributes(PrintWriter pw)

{

pw.println(itemCode);

pw.println(type);

pw.println(variety);

}

}

and in the fruit class (I also have a veg class but that is the same as the fruit):

import java.io.*;

publicclass Fruitextends Item

{

privatedouble stockLevel;

privatedouble pricePerKilo;

public Fruit()

{

super();

stockLevel = 0;

pricePerKilo = 0;

}

public Fruit(String code, String type, String variety,

double stockLevel,double pricePerKilo)

{

super(code, type, variety);

this.stockLevel = stockLevel;

this.pricePerKilo = pricePerKilo;

}

publicdouble getStockLevel()

{

return stockLevel;

}

publicvoid readAttributes(BufferedReader br)throws IOException

{

super.readAttributes(br);

this.stockLevel = Double.parseDouble(br.readLine());

this.pricePerKilo= Double.parseDouble(br.readLine());

}

publicvoid writeAttributes(PrintWriter pw)

{

super.writeAttributes(pw);

pw.println(stockLevel);

pw.println(pricePerKilo);

}

publicvoid writeObject(PrintWriter pw)

{

pw.println("Fruit");

writeAttributes(pw);

}

}

I think that some of my readAttributes() are dodgy. As i am ably to open the file with the program but that is as far as i can get with it.

Any help would be appreciated.

CC

[8266 byte] By [cookie_crumblea] at [2007-11-27 4:30:32]
# 1

> I think that some of my readAttributes() are dodgy

What does that mean? You will have to give us some more info. Do you get compiler errors? Do you get runtime errors? Are the values you read in wrong?

The more info you give us, the easier it is for someone to track down your problem.

floundera at 2007-7-12 9:39:50 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi

thanks here is more info

I want to load in the data from the file:

Read until teh end of the file:

while (inputLine != null)

the program should read through it and sort out the lines that are "fruit" and those that are "vegetable".

If Fruit create an empty fruit object by using the default fruit constructor

if (inputLine.equals("Fruit"))

{

temp = new Fruit();

}

Same for vegetable

Pass the attributes into the generic Item class

public void readAttributes(BufferedReader br) throws IOException

{

this.itemCode = br.readLine();

this.type = br.readLine();

this.variety = br.readLine();

}

Continue passing the data into the specific item class (fruit or Veg)

public void readAttributes(BufferedReader br) throws IOException

{

super.readAttributes(br);

this.unitsInStock = Integer.parseInt(br.readLine());

this.unitPrice = Double.parseDouble(br.readLine());

Store the temp data into the array as long as it is not the end of the file

by bringing back the data from the data classes and storing.

if (temp != null)

{

temp.readAttributes(br);

items[index] = temp;

index ++;

inputLine = br.readLine();

numberOfRecords = index;

}

br.close();

Then close the file

The problem is that although i can open the file OK i am having trouble passing the data into the data classes and storing it in the array

Which means i cant use the data in the application.

Cheers

CC

cookie_crumblea at 2007-7-12 9:39:50 > top of Java-index,Java Essentials,Java Programming...
# 3
> i am having troubleInsufficient information. Return to customer for clarification. What trouble? You've been asked this already once.
ejpa at 2007-7-12 9:39:50 > top of Java-index,Java Essentials,Java Programming...
# 4

ok your problem is that each time you call br.readLine() it reads the next line in

grocery.dat.

i dont know how grocery.dat is structured i.e. how the data is saved in the file so i cant comment on how u are accessing the the paramters associated with each item in the file

if you could show that file it would be helpful

bouncera at 2007-7-12 9:39:50 > top of Java-index,Java Essentials,Java Programming...
# 5
I think you might also have a vegetable class oh resourceful one.C
CraigMaybea at 2007-7-12 9:39:50 > top of Java-index,Java Essentials,Java Programming...
# 6
lol
Java_KING2k7a at 2007-7-12 9:39:50 > top of Java-index,Java Essentials,Java Programming...