compiles but does not run

Getting this error when running this program after it compiles:

java.util.NoSuchElementException

at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)

at MaleFemaleGPA.main(MaleFemaleGPA.java:30)

Here is my code:

import java.io.*;

import java.util.*;

publicclass MaleFemaleGPA

{

publicstaticvoid main(String[] args)

throws IOException

{

int countFemale = 0;

int countMale = 0;

double sumFemaleGPA = 0.0,

sumMaleGPA = 0.0,

averageFemaleGPA = 0.0,

averageMaleGPA = 0.0;

char letterCode;

double GPA;

String inputLine;

StringTokenizer tokenizer =null;

Scanner inFile =new Scanner(new FileReader("GPAdata.dat"));

inputLine = inFile.nextLine();

while(inputLine !=null)

{

tokenizer =new StringTokenizer(inputLine);

letterCode = tokenizer.nextToken().charAt(0);

GPA = Double.parseDouble(tokenizer.nextToken());

switch(letterCode)

{

case'f': countFemale++;

sumFemaleGPA = sumFemaleGPA + GPA;

break;

case'm': countMale++;

sumMaleGPA = sumMaleGPA + GPA;

break;

default: System.out.println("Bad data.");

}

inputLine = inFile.nextLine();

}

if(countFemale != 0)

averageFemaleGPA = sumFemaleGPA / countFemale;

if(countMale != 0)

averageMaleGPA = sumMaleGPA / countMale;

System.out.println("Average female GPA = " + averageFemaleGPA);

System.out.println("Average male GPA = " + averageMaleGPA);

System.out.println();

}

}

[2887 byte] By [mattvgta] at [2007-11-27 10:36:46]
# 1

The error message is telling you exactly what's wrong.

At line 332, you're calling nextToken, but there are no tokens left.

jverda at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 2

If I was getting this exception, the first thing I would want to know was what caused it.

Perhaps print the value of inputLine. Such astry {

letterCode = tokenizer.nextToken().charAt(0);

GPA = Double.parseDouble(tokenizer.nextToken());

} catch (NoSuchElementException e) {

System.out.println(inputLine);

throw e;

}

bheilersa at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 3

I tried bheilers suggestion but still get the same error. My file is formatted like this:

m 3.0

f 2.5

f 2.1

m 4.0

etc.....

Not sure why it's saying there is no token.

mattvgta at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 4

> I tried bheilers suggestion but still get the same

> error. My file is formatted like this:

>

> m 3.0

> f 2.5

> f 2.1

> m 4.0

>

> etc.....

>

> Not sure why it's saying there is no token.

That answer is easy and already given: because there is no token. Java is not making it up, really. If you think there is one, you are mistaken. Ensure yourself that there really is no token left by printing out each line before tokenizing it: you will see what is going wrong then (this is called debugging).

prometheuzza at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 5

So I put: System.out.println("Token is " + inputLine);

and it prints the first line of the file "m 3.82". That tells me there is a token, right?

mattvgta at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 6

> So I put: System.out.println("Token is " +

> inputLine);

>

> and it prints the first line of the file "m 3.82".

> That tells me there is a token, right?

Yes, at that point there is. Put in some more to see where it does happen.

prometheuzza at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 7

Could it be that it doesn't know when the EOF is?

mattvgta at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 8

> Could it be that it doesn't know when the EOF is?

It knows that.

But the error lies probably in your code. You're trying to read a last line which simply isn't there. Try parsing your file like this instead:

while(inFile.hasNextLine())

{

String inputLine = inFile.nextLine();

// ...

}

prometheuzza at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 9

I used:

while(inFile.hasNextLine())

{

String inputLine = inFile.nextLine();

// ...

}

and got the same error so I put String inputLine = inFile.nextLine();

above the while loop and it worked.

Thanks a lot.

mattvgta at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 10

> I used:

>

> while(inFile.hasNextLine())

> {

>String inputLine = inFile.nextLine();

> // ...

> }

>

> and got the same error so I put String

> inputLine = inFile.nextLine();

above the while

> loop and it worked.

>

> Thanks a lot.

Then you're now probably doing:

String inputLine = inFile.nextLine();

while(inFile.hasNextLine())

{

// all of your parsing

inputLine = inFile.nextLine();

}

While I meant this (which is a cleaner approach IMO):

while(inFile.hasNextLine())

{

String inputLine = inFile.nextLine(); // first thing you do is read a line!

// all of your parsing

}

And you're welcome.

prometheuzza at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...
# 11

> I used:

>

> > while(inFile.hasNextLine())

> {

>String inputLine = inFile.nextLine();

> // ...

> }

>

>

> and got the same error so I put String

> inputLine = inFile.nextLine();

above the while

> loop and it worked.

I'm glad you got it working, but it sounds like you just guessed until you got lucky, but don't understand what you were doing wrong and why there were no more tokens in the other case and why it works now.

jverda at 2007-7-28 18:43:39 > top of Java-index,Java Essentials,New To Java...