Search code examples
javajava.util.scannerinputmismatchexception

Getting InputMismatchException when reading an int from a file with Scanner


I am working on a program which imports a library from a generated file. The file generates properly and is found by Scanner. The first line has a single int as written by

    pw.println(cdarchive.getNumber());

Elsewhere in the code. This part seems to work fine.

This is the error I'm getting:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at no.hib.dat102.IO.readFile(IO.java:26)
    at no.hib.dat102.Menu.start(Menu.java:34)
    at no.hib.dat102.CdArchiveClient.main(CdArchiveClient.java:10)

The line it refers to is

    int libSize = in.nextInt(); 

This is my method:

public class IO {

    static final String DELIMITER = "#";
    public static CdArchiveADT readFile(String filename) {
        Scanner in = null;
        CdArchiveADT cda = null;
        try 
        {
            File f = new File(filename+".txt");
            in = new Scanner(f);
            System.out.println(f);
            in.useDelimiter(DELIMITER);
            int libSize = in.nextInt(); 
            System.out.println("libSize" + libSize);
            cda = new CdArchive(libSize);
            for (int i=0; i<libSize;i++) {
                int inId = in.nextInt();
                String inTitle= in.next();
                String inArtist = in.next();
                String inLabel = in.next();
                String inGenre = in.next();
                int inYear = in.nextInt();
                in.nextLine();
                cda.addCd(new CD(inId, inArtist, inTitle, inYear, inGenre, inLabel));
                System.out.println("Closing Scanner (input)");
                in.close(); 

            }
    }
        catch (FileNotFoundException e){
            System.out.println("Config file not found!");
            e.printStackTrace();
        }


        return cda;

    }

EDIT:

This is the method that writes to the file:
    public static void writeFile(CdArchiveADT cdarchive, String filename) throws IOException {      
        PrintWriter pw = null;
        File file = null;

        try {
            file = new File(filename +".txt");
            // Create the file if it does not already exist
            file.createNewFile();

            // Writing metadata
            pw = new PrintWriter(new FileWriter(file, false));
            pw.println(cdarchive.getNumber());
            // Writing data, if CdArchive is not empty
            if (cdarchive.getCdTable()[0] != null) {
            for (int i = 0; i<cdarchive.getNumber(); i++ ) {
                CD c = cdarchive.getCdTable()[i];
                pw.print(c.getId()); pw.print(DELIMITER);
                pw.print(c.getTitle()); pw.print(DELIMITER);
                pw.print(c.getArtist()); pw.print(DELIMITER);
                pw.print(c.getLabel()); pw.print(DELIMITER);
                pw.print(c.getGenre()); pw.print(DELIMITER);
                pw.print(c.getYear()); pw.println(DELIMITER); 
            }
        }
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found!");
            e.printStackTrace();
        }
        finally
        {
            if ( pw != null ) 
            {
                System.out.println("Closing PrintWriter");
                pw.close();
            }
        }
    }

Solution

  • I got a working example:

    public static void main(String[] args) {
    
        // write
        String delimiter = "#";
        StringWriter stringWriter = new StringWriter();
        PrintWriter pw = new PrintWriter(stringWriter);
        pw.println(3);
        for (int i = 0; i < 3; i++) {
            pw.print("id " + i);
            pw.print(delimiter);
            pw.print("titel " + i);
            pw.print(delimiter);
            pw.print("artist " + i);
            pw.println(delimiter);
        }
    
        String theString = stringWriter.toString();
        System.out.println(theString);
    
        try {
            pw.close();
            stringWriter.close();
        } catch (IOException e) {
            // ignore in example
        }
    
        // read
        Scanner in = new Scanner(theString);
        in.useDelimiter("\\s*#\\s*|\\s*\n\\s*"); // add new line as delimiter aswell
        int libSize = in.nextInt();
        for (int i = 0; i < libSize; i++) {
            String inId = in.next();
            String inTitle = in.next();
            String inArtist = in.next();
            in.nextLine();
    
            System.out.println("read: " + inId + ", " + inTitle + ", " + inArtist);
        }
        in.close();
    }
    

    The point is to add new line to the used delimiters aswell