Help with String Tokenizer Class.........

public void readFile() throws IOException

{

inputstream = new BufferedReader(new FileReader("C:\\CarInputData.txt"));

int idx = 0; int tokenCount;

String words[] = new String [500];

StringTokenizer st;

String line = inputstream.readLine();

while (line!= null)

{

st = new StringTokenizer(line,":");

tokenCount = st.countTokens();

System.out.println(st.nextToken());

while (st.hasMoreTokens()) // make sure there is stuff to get

{

words[idx] = st.nextToken();

idx++;

}

for (idx=0;idx<tokenCount; idx++)

{

System.out.println(words[idx]);

}

line = inputstream.readLine();

idx=0;

}

}

Input file CarInputData.txt:

Focus Wagon ZTW model:

BasePrice-18,445:

Color:c1.Fort Knox Gold Clearcoat Metallic:c2.Liquid Grey Clearcoat Metallic:c3.Infra-Red Clearcoat:c4.Grabber Green Clearcoat Metallic:c5.Sangria Red Clearcoat Metallic:c6.French Blue Clearcoat Metallic:c7.Twilight Blue Clearcoat Metallic:c8.CD Silver Clearcoat Metallic:c9.Pitch Black Clearcoat:c10.Cloud 9 White Clearcoat:

Transmission:t1.Automatic-$0:t2.Manual-$-815:

Brakes/TractionControl:b1.Standard-$0:b2.ABS-$425:b3.ABS with Advance Trac-$1625:

Side Impact Air Bags:a1.present-$0:a2.Not Present-$350:

Power Moonroof:p1.Present-$0:p2.Not present:$595

Output:

Focus Wagon ZTW model

null

BasePrice-18,445

null

Color

c1.Fort Knox Gold Clearcoat Metallic

c2.Liquid Grey Clearcoat Metallic

c3.Infra-Red Clearcoat

c4.Grabber Green Clearcoat Metallic

c5.Sangria Red Clearcoat Metallic

c6.French Blue Clearcoat Metallic

c7.Twilight Blue Clearcoat Metallic

c8.CD Silver Clearcoat Metallic

c9.Pitch Black Clearcoat

c10.Cloud 9 White Clearcoat

null

Transmission

t1.Automatic-$0

t2.Manual-$-815

c3.Infra-Red Clearcoat

Brakes/TractionControl

b1.Standard-$0

b2.ABS-$425Exception in thread "main" java.util.NoSuchElementException

at java.util.StringTokenizer.nextToken(Unknown Source)

at Automotive.Automotive.readFile(Automotive.java:36)

at Automotive.Main.main(Main.java:16)

b3.ABS with Advance Trac-$1625

c4.Grabber Green Clearcoat Metallic

Side Impact Air Bags

a1.present-$0

a2.Not Present-$350

b3.ABS with Advance Trac-$1625

Power Moonroof

p1.Present-$0

p2.Not present

$595

c4.Grabber Green Clearcoat Metallic

My question is in Transmission after t1 and t2 it is again displaying another option called c3.....

Similar is the case with other options too.......

Can anyone tell where iam going wrong?>

[2807 byte] By [GitaSumana] at [2007-11-27 1:41:18]
# 1

I don't see how you are getting an exception in the middle of your output and yet your program continues. are you pasting the correct output?

Also, try this instead

for (int i = 0 ; i < idx.length ; i++)//for(idx=0;idx<tokenCount; idx++)

{

System.out.println(words[i]);

}

Message was edited by:

desimunda>

desimundaa at 2007-7-12 0:56:35 > top of Java-index,Java Essentials,Java Programming...
# 2

You're writing too much code my friend.

Just try this:

BufferedReader inputstream = null;

public void readFile() throws IOException {

inputstream = new BufferedReader(new FileReader("C:\\CarInputData.txt"));

StringTokenizer st;

String line = null;

while ((line = inputstream.readLine()) != null) {

st = new StringTokenizer(line,":");

while (st.hasMoreTokens())

System.out.println(st.nextToken());

}

}

You can check this little example:

http://www.javadb.com/using-the-stringtokenizer-class

Cheers,

Nick

Message was edited by:

eurogolfer

(forgot to remove a couple of variable declarations)

eurogolfera at 2007-7-12 0:56:35 > top of Java-index,Java Essentials,Java Programming...