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]

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;
}
> 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).
> 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.
> 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 >
