Can't get my file to read correctly..  keep getting mismatch error

below will be my code and the file it is supposed to read.. I can't get it to
work. It keeps giving me a mismatch error.
import java.util.*;
import java.io.*;
public class TestStudent{
public static void main(String[] args){
try {
Scanner s = new Scanner(new File("StudentGrades.txt"));
if(s.hasNext()){
String title = s.next(); //throws away the title line
s.useDelimiter("[,\\n+]");
while(s.hasNext()){
String name = s.next();
int id = s.nextInt();
double attGr = s.nextDouble();
double hwGr = s.nextDouble();
double e1Gr = s.nextDouble();
double e2Gr = s.nextDouble();
double fExGr = s.nextDouble();
System.out.println(name+attGr+hwGr+e1Gr+e2Gr+fExGr);
//Construct the Student object based on the above data
catch(Exception e){
System.out.println(e);
file:: Studentgrades.txt has this info in it
Name,id,attGr,HkGr,E1Gr,E2Gr,FExGr
Alex Smith,111111,75,80,75,78,73
Mary Alexander,222222,95,90,87,93,85
Johnson White,333333,80,80,75,67,73
Andrew Black,44444,95,85,86,93,95
Gary Shippment,55555,86,78,87,72,68
Walter Green,666666,67,90,90,95,85
Mario Winter,777777,95,90,95,87,92
Henry Summer,888888,87,100,85,83,72
Michel Spring,999999,56,100,75,78,85
April May,112222,40,85,65,55,65
May Spring,113333,25,65,50,45,50
Helen Hunter,114444,79,88,66,73,67
Ammy Gram,115555,87,90,90,87,95
Bill Bonnet,223333,95,100,95,82,95
Larry Smith,224444,100,87,73,80,76
Julia Winter,334444,35,75,55,63,70
Suzan Snow,335555,67,65,40,65,60
Gary Carpenter,336666,98,80,65,78,83
Last Not Least,337777,97,90,87,94,88

Hi,
forget my previous answer.
You have mistake in your pattern, so I do it by different way. Instead of doubles I put ints, because all your numbers are int. However you can put doubles there as well.
So the code:
import java.util.*;
import java.io.*;
public class TestStudent{
    public static void main(String[] args){
        try {
            Scanner scanner = new Scanner(new File("StudentGrades.txt"));
            if(scanner.hasNext()){
                String title = scanner.next(); //throws away the title line
            scanner.useDelimiter(System.getProperty("line.separator"));
            while (scanner.hasNext()) {
                parseLine(scanner.next());
        catch(Exception e){
            System.out.println(e);
    private static void parseLine(String line) {
        Scanner lineScanner = new Scanner(line);
        lineScanner.useDelimiter(",");
        String name = lineScanner.next();
        int id = lineScanner.nextInt();
        int attGr = lineScanner.nextInt();
        int hwGr = lineScanner.nextInt();
        int e1Gr = lineScanner.nextInt();
        int e2Gr = lineScanner.nextInt();
        int fExGr = lineScanner.nextInt();
        System.out.println(name+attGr+hwGr+e1Gr+e2Gr+fExGr);
}L.P.

Similar Messages

Maybe you are looking for