at end of tether! Reading in variables from a text file

Hi all

My stress factor has gone through the roof because I am trying to read in from a text file (you may have seen some earlir questions about ArrayLists) it's just not working!

The code is below. The result is that it's reading in all of the car data, none of the motorbike and only the first line for the services. It's odd and it's driving me insane!

Here's an example of the data it's reading. There are about 7-10 sets of data per type

<car><reg>AB04CDE</reg><make>Ford</make><model>Fiesta</model><colour>blue</colour><passenger_no>4</passenger_no></car>

<service><service_no>13570</service_no><reg>J605PLE</reg><date>15:07:2006</date><miles>20000</miles><part_replaced>brake_pads</part_replaced><part_replaced>front_tyres</part_replaced></service>

<motorbike><reg>TT05EKJ</reg><make>Triumph</make><model>Speedmaster</model><colour>black</colour><load>20.50</load></motorbike>

Here's the code

while (moreToRead){

String line;

try{

line = fileReader.getNextStructure();

// collect the data from the file

if (line.indexOf("<car>")> -1){

// Select/Extract the registration element

int nStart = line.indexOf("<reg>");

int nEnd = line.indexOf("</reg>");

String reg = line.substring(nStart+5,nEnd);

// Select/Extract the make element

nStart = line.indexOf("<make>");

nEnd = line.indexOf("</make>");

String make = line.substring(nStart+6,nEnd);

// Select/Extract the model element

nStart = line.indexOf("<model>");

nEnd = line.indexOf("</model>");

String model = line.substring(nStart+7,nEnd);

// Select/Extract the colour element

nStart = line.indexOf("<colour>");

nEnd = line.indexOf("</colour>");

String colour = line.substring(nStart+8,nEnd);

// Select/Extract the passenger_no element

nStart = line.indexOf("<passenger_no>");

nEnd = line.indexOf("</passenger_no>");

String passenger_no = line.substring(nStart+14,nEnd);

//convert string to int

int passengerInt = Integer.parseInt(passenger_no);

// declare new object car and assign the variables then add it to the array.

Car c =new Car (reg, make, model, colour, passengerInt);

carList.add(c);

}elseif (line.indexOf("<bike>")> -1){

// Select/Extract the registration element

int nStart = line.indexOf("<reg>");

int nEnd = line.indexOf("</reg>");

String reg = line.substring(nStart+5,nEnd);

// Select/Extract the make element

nStart = line.indexOf("<make>");

nEnd = line.indexOf("</make>");

String make = line.substring(nStart+6,nEnd);

// Select/Extract the model element

nStart = line.indexOf("<model>");

nEnd = line.indexOf("</model>");

String model = line.substring(nStart+7,nEnd);

// Select/Extract the colour element

nStart = line.indexOf("<colour>");

nEnd = line.indexOf("</colour>");

String colour = line.substring(nStart+8,nEnd);

// Select/Extract the load element

nStart = line.indexOf("<load>");

nEnd = line.indexOf("</load>");

String load = line.substring(nStart+6,nEnd);

//convert load string to double

double bikeLoad = Double.parseDouble(load);

// declare new object motorbike and assign the variables then add it to the array.

Motorbike m =new Motorbike (reg, make, model, colour, bikeLoad);

bikeList.add(m);

}else{

// Select/Extract the service_number element

int nStart = line.indexOf("<service_no>");

int nEnd = line.indexOf("</service_no>");

String service_no = line.substring(nStart+12,nEnd);

console.println("service = " + service_no);

nStart = line.indexOf("<reg>");

nEnd = line.indexOf("</reg>");

String reg = line.substring(nStart+5,nEnd);

console.println("service = " + reg);

nStart = line.indexOf("<date>");

nEnd = line.indexOf("</date>");

String date = line.substring(nStart+6,nEnd);

console.println("service = " + date);

nStart = line.indexOf("<miles>");

nEnd = line.indexOf("</miles>");

String miles = line.substring(nStart+7,nEnd);

console.println("service = " + miles);

nStart = line.indexOf("<part_replaced>");

nEnd = line.indexOf("</part_replaced>");

String part_replaced = line.substring(nStart+15,nEnd);

console.println("service = " + part_replaced);

//convert string to int

int dateOfService = Integer.parseInt(date);

//convert string to double

double milesAtService = Double.parseDouble(miles);

//convert service no to unique int

int serviceNo = Integer.parseInt(service_no);

// declare new object service and assign the variables then add it to the array.

Service s =new Service (reg, part_replaced, serviceNo, dateOfService, milesAtService);

serviceList.add(s);

}

}

catch (Exception e){

// Run out of data

moreToRead =false;

}

}

If anyone can spy anything that could be causing this I love your advice. I simply can't see it.

Jo

[8249 byte] By [Joa] at [2007-11-26 16:49:03]
# 1
maybe digester or xstream
mchan0a at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 2
Sorry, I don't understand your reply
Joa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 3

It's not reading your bike data because your code is looking for <bike> but your data starts with <motorbike>

And when it finds the <motorbike> line, it will go into the code that's supposed to handle services and you will probably get a IndexOutOfBounds exception because endpos will be -1.

You need to change your code so that you don't assume that anything that isn't a car or bike is a service.

You should also print out the stack trace in your exception handler rather than just existing the loop.

passgodeva at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 4

thank you.

regarding the bike/motorbike - Doh, how could i have missed that!!

I have amended the final else to } else if (line.indexOf("<service>")> -1){

I must admit that my tutor gave the group this line of code in an example so i'm not 100% sure what the >-1 bit means! so i'm not sure what you mean about when it finds motorbikes it goes to the service code?

Also, wht is a stack trace?

apologies if im being stupid

jo

Joa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 5
String.indexOf returns a -1 if it doesnt find the string you are searching for, and it returns the position if it does find it. So the > -1 just checks to see if the string <car> or whatever is in the line.
michael.paynea at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 6
@OP: is that supposed to be xml? If so, why don't you pre/append thenecessary ritual voodoo text and let a SAX parser do all the work?kind regards,Jos
JosAHa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 7
thanks michaelhi jos, we have been asked not to use a parser for this assignment. evil tutor i think! loleven with the changes I have made explained above, it's still not reading in motorbike or anything after the first service dataset
Joa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 8

> hi jos,

> we have been asked not to use a parser for this assignment. evil

> tutor i think! lol

Your example seems to imply that all the <tag> ... </tag> pairs have

to occur on a single line; if that is so, you can do some cheap

programming like this:String getText(String line, String tag) {

int start= line.indexOf("<"+tag+">");

int end= line.indexOf("</"+tag+">", start);

if (start < 0 || end < 0) return null; // no <tag> ... </tag> pair found

// return the text in between the <tag> ... </tag> tags

return line.substring(start+tag.length()+2, end);

}

kind regards,

Jos

JosAHa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 9

Change

catch (Exception e) {

// Run out of data

moreToRead = false;

}

to

catch (Exception e) {

// Run out of data

e.printStackTrace();

moreToRead = false;

}

I think you'll find you are getting a NumberFormatException because this line

int dateOfService = Integer.parseInt(date);

is trying to parse 15:07:2006 which is not a valid integer.

passgodeva at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 10

passgodev - yes, thats exactly what has happened.

According to the assignment notes the dates are always in the form dd:mm:yyyy and are composed of 3 integers.

So I'm not sure how else to handle this (other than cheat and put the motorbike data ahead of the service in the text file!!)

Joa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 11

Well if you want to split the date into three integers you could do this

String[] splitDate = date.split(":");

int day = Integer.parseInt(splitDate[0]);

int month = Integer.parseInt(splitDate[1]);

int year = Integer.parseInt(splitDate[2]);

but you would then have to change your Service class constructor to accept all these values.

What does your Service class do with the date ?

passgodeva at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 12

we have been given a class:

public Date(int day,int month,int year) {..................

but to be honest, using the service class gets a higher pass and as it is quite clear that java or indeed programming is not for me I just need the basic pass so will go with the cheat of moving the motorbike and service data around.

many thanks for your help, I just need to push on with finalising the methods now

Joa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 13

Change your Service class constructor so that dateOfService is a Date instead of an int, then do

String[] splitDate = date.split(":");

int day = Integer.parseInt(splitDate[0]);

int month = Integer.parseInt(splitDate[1]);

int year = Integer.parseInt(splitDate[2]);

Date dateOfService = new Date(day, month, year)

double milesAtService = Double.parseDouble(miles);

int serviceNo = Integer.parseInt(service_no);

// declare new object service and assign the variables then add it to the array.

Service s = new Service (reg, part_replaced, serviceNo, dateOfService, milesAtService);

passgodeva at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...
# 14
It always gets to me how assignments like this (with artificial constraints like "don't use a parser", as you mentioned) tend to teach people rather daft ways of doing things.
DavidKNa at 2007-7-8 23:16:35 > top of Java-index,Java Essentials,New To Java...