1

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();
            }
        }
    }
Vincent Vega
  • 119
  • 10
  • Did you verify the format of the written file? The InputMismatchException indicates, that the token you try to read can not be interpreted as an integer. – Franz Deschler Feb 01 '17 at 07:42
  • That `in.close()` *inside* the loop is going to kill your program, as soon as you get past the current hurdle. Suggest you use try-with-resources instead. – Andreas Feb 01 '17 at 08:09
  • Show example of the input file, so we can better tell you why `#` is not a good delimiter for a file containing line terminators. E.g. if the first line is a number, e.g `42`, and the second line is `#` delimited, e.g. `1#Title#Artist#Label#Genre#2017`, then the first *token* is the text `42\r\n1`, which is definitely not a valid number. – Andreas Feb 01 '17 at 08:12
  • Thanks for your replies. Here is an example of a line written: `1` `1#Example Title#Example Artist#Example Label#CHRISTIAN#1999#`. Cant seem to do a
    in comments, but they are separate lines.
    – Vincent Vega Feb 01 '17 at 09:16

2 Answers2

0

try to use

static final String DELIMITER = "\\s*#\\s*";

Otherwise any leading or trailing spaces will cause that error.

kamehl23
  • 522
  • 3
  • 6
  • Unlikely to fix the problem, as the lines of input do not appear to be separated by `#`. Of course, not seeing the actual input, it's uncertain what the input is. – Andreas Feb 01 '17 at 08:17
  • That did unfortunately not fix the problem for me. – Vincent Vega Feb 01 '17 at 09:21
0

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

kamehl23
  • 522
  • 3
  • 6
  • This did indeed work! The output wasn't so pretty. `3 0\s*#\s*|\s* \s*Title 1\s*#\s*|\s* \s*Artist 1\s*#\s*|\s* \s*Label 1\s*#\s*|\s* \s*ROCK\s*#\s*|\s* \s*1\s*#\s*|\s* \s* 1\s*#\s*|\s* \s*Title 2\s*#\s*|\s* \s*Artist 2\s*#\s*|\s* \s*Label 2\s*#\s*|\s* \s*ALTERNATIVE\s*#\s*|\s* \s*2000\s*#\s*|\s* \s* 2\s*#\s*|\s* \s*Title 3\s*#\s*|\s* \s*Artist 3\s*#\s*|\s* \s*Label 3\s*#\s*|\s* \s*POP\s*#\s*|\s* \s*1\s*#\s*|\s* \s* ` – Vincent Vega Feb 01 '17 at 11:05
  • I did this and now everything is working perfectly. Thank you for your time and valuable advice! – Vincent Vega Feb 01 '17 at 12:05