Re: INPUTMISMATCHEXCEPTION
Hi, Give me that studentGPA.txt file..then only I can check what happend
Re: INPUTMISMATCHEXCEPTION
The InputMismatchException is usually thrown because you are trying to use a method on the Scanner object which returns a specific type such as nextDouble() but when the next token isn't a double it throws the exception at you.
You can always check what the next token is before trying to grab it using Scanner.hasNextDouble().
See Scanner (Java Platform SE 6))
// Json
Re: INPUTMISMATCHEXCEPTION
hey, jassi here is the text file :
M 3.50
M 4.31
F 3.46
M 2.98
F 4.10
M 3.10
F 4.23
M 2.99
F 3.65
F 2.53
Re: INPUTMISMATCHEXCEPTION
Can you please elaborate what is your requirement??
What must be the output you want from your program?
Actually Json is correct as the value you are reading from the file is character and double so you are mismatching input by reading char value in double.
Re: INPUTMISMATCHEXCEPTION
Well jassi the requirement is this:
You receve a file that contains female and male student's GPA for certain courses .Due to confidentility, only the letter code f is used to represent female students; m is used to repesent male students.Every file entry consists of a letter code followed by a GPA .Each line has one entry The number of entries that may be in a file is unknown. Write a program that computes and outputs the average GPA for both female and male students Format your results to two decimal places.
Re: INPUTMISMATCHEXCEPTION
Well I have made some changes to my program.But i still have this error:
[
import java.util.*;
import java.io.*;
public class university
{
public static void main(String[] args)throws FileNotFoundException
{
double GPA ;
double sum = 0;
String Mcode ;
String Fcode ;
Scanner inFile = new Scanner(new FileReader("studentGPA.txt"));
PrintWriter outFile = new PrintWriter("studentGPAavg.out");
Mcode = inFile.next();
Fcode = inFile.next();
outFile.println("Enter code:" + Mcode + " " + Fcode );
outFile.print("Students GPA is:" );
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
outFile.println();
outFile.printf("The average of the GPA is: %5.2f %n ", sum/10.0);
inFile.close();
outFile.close();
}
}
/]
here is the error:
C:\TEMP>java university
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:817)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at university.main(university.java:64)
Re: INPUTMISMATCHEXCEPTION
Resolved Program without errors.After running your program
You can check studentGPavg.out
import java.util.*;
import java.io.*;
public class university
{
public static void main(String[] args)throws FileNotFoundException
{
double GPA ;
double sum = 0;
String gender ;
float counter=0;
Scanner inFile = new Scanner(new FileReader("studentGPA.txt"));
PrintWriter outFile = new PrintWriter("studentGPAavg.out");
outFile.println("Code: M F");
outFile.println("Students GPA is:" );
while(inFile.hasNext()){
gender=inFile.next();
if(gender.equalsIgnoreCase("M")){
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
counter++;
}else if(gender.equalsIgnoreCase("F")){
GPA = inFile.nextDouble();
outFile.printf(" %5.2f", GPA);
sum = sum + GPA;
counter++;
}
}
outFile.println();
outFile.printf("The average of the GPA is: %5.2f %n ", sum/counter);
inFile.close();
outFile.close();
}
}
Click on Thanks link. and mark your thread as resolved.