简体   繁体   中英

java.util.InputMismatchException in java

import java.util.*;
import java.lang.*;
import java.io.*;

public class MinorAssignment_PartB {

public static void main(String[] args) throws Exception {


    List<StudentMarks> marks = new ArrayList<StudentMarks>();
    String File = "studentinfo.txt";
    Scanner scan = new Scanner(new File(File));
    scan.useDelimiter("[,|\\n]");

    while(scan.hasNext()){

    //the error refers to this part here

    marks.add(new StudentMarks(scan.next(), scan.next(), scan.nextDouble(),   scan.nextDouble(), scan.nextDouble(), scan.nextDouble()));

    System.out.printf("%-15s %-15s %-15s %-15s %-15s %-15s %-15s %-15s %n", "Student Name", "Student Fan", "Part A", "Part B", "Part C", "Part D", "Mark", "Grade");

        for (int i = 0; i < marks.size()-1; i++) {
            System.out.println(marks.get(i));

        }
    }


  }
}

I'm not sure how to fix it, it is while looping and reading from a text file with 2 strings then 4 doubles separated by commas, and there is 10 lines to loop through.

Any help would be much appreciated.

this is what is in studentinfo.txt but each new person is on one line

Adam Adamson,adam0001,85.4,79.8,82.4,86.1 Bethany Bright,brig0001,89.7,85.6,84.2,82.9 Cameron Carlson,carl0001,55.45,49.82,60.4,42.27 David Dawson,daws0001,72.6,78.49,80.2,65.88 Evelyn Ellis,elli0001,50.2,35.88,48.41,58.37 Frances Fitz,fitz0001,78.9,75.67,82.48,79.1 Greg Gregson,greg0001,24.3,32.88,29.72,28.4 Harriett Hope,hope0001,52.2,58.93,61.5,63.44 Ivan Indigo,indi0001,88.4,91.23,90.05,92.46 Jessica Jones,jone0001,82.33,89.74,81.3,84.85

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. If you use delimiter, than scan.hasNext() works on that delimiter only. To solve your problem use comma , at the end of your txt file line. And use following delimiter.

scan.useDelimiter(",");

studentinfo.txt file should be

Adam Adamson,adam0001,85.4,79.8,82.4,86.1,
Bethany Bright,brig0001,89.7,85.6,84.2,82.9,
Cameron Carlson,carl0001,55.45,49.82,60.4,42.27,
David Dawson,daws0001,72.6,78.49,80.2,65.88,
Evelyn Ellis,elli0001,50.2,35.88,48.41,58.37,
Frances Fitz,fitz0001,78.9,75.67,82.48,79.1,
Greg Gregson,greg0001,24.3,32.88,29.72,28.4,
Harriett Hope,hope0001,52.2,58.93,61.5,63.44,
Ivan Indigo,indi0001,88.4,91.23,90.05,92.46,
Jessica Jones,jone0001,82.33,89.74,81.3,84.85,

Maybe your line-endings matter (look at the delimiters):

    // works for \n line ending
    Scanner scan = new Scanner("A,B,0,0,0,0\nC,D,1,1,1,1\n");
    scan.useDelimiter("[,|\\n]");
    while (scan.hasNext()) {
        System.out.print(scan.next());
        System.out.print(scan.next());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.println();
    }
    scan.close();

    // works for both \r\n and \n line endings
    scan = new Scanner("A,B,0,0,0,0\nC,D,1,1,1,1\r\n");
    scan.useDelimiter(",|\\r?\\n");
    while (scan.hasNext()) {
        System.out.print(scan.next());
        System.out.print(scan.next());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.print(scan.nextDouble());
        System.out.println();
    }
    scan.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM