0

I'm receiving an InputMismatchException while scanning in the last int of a csv.

public class TradeSim {
    public void readFile(String fileName) {
        try {
            // file name and absolute path
            File file = new File(fileName);
            String fullPath = file.getAbsolutePath();
            System.out.println("Using file:\n" + fullPath + "\n");

            // set File Input Stream to Full Path
            FileInputStream inputStream = new FileInputStream(fullPath);

            // open input stream and retrieve data
            Scanner scanner = new Scanner(inputStream);
            scanner.useDelimiter(System.getProperty("line.separator"));
            while (scanner.hasNext()) {
                String data = scanner.next(); // gets a whole line
                System.out.println(data);
                parseCSVLine(data);
            }
            scanner.close();
        } catch (Exception e) {
            System.out.println("Encountered Error reading file:\n" + e.toString() + "\n");
        }
    }

    public void parseCSVLine(String line) {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(",");
        long timeStamp = scanner.nextLong();
        System.out.println("timeStamp: " + timeStamp);
        String symbol = scanner.next();
        System.out.println("symbol: " + symbol);
        int quantity = scanner.nextInt();
        System.out.println("quantity: " + quantity);
        int price = scanner.nextInt(); //Error occurs here!!
        System.out.println("price: " + price);
        Trades trades = new Trades(timeStamp, symbol, quantity, price);
    }
}

File content:

51300051409,fbc,273,297
51300073658,cef,250,262

Output:

Using file:
/Users/andrewkeithly/netbeansprojects/Trades Exercise/input.csv

51300051409,fbc,273,297
timeStamp: 51300051409
symbol: fbc
quantity: 273
Encountered Error reading file:
java.util.InputMismatchException

Tom
  • 16,842
  • 17
  • 45
  • 54
andrewkeithly
  • 133
  • 11
  • maybe its not an int at that position. include the whole scanning process in a try catch block first of all and preferably use scanner.next(). later you can parse as needed – AbtPst Oct 22 '15 at 20:10
  • @AbtPst Thanks for your response! The line it is reading is: <51300051409,fbc,273,297> It scans the first 3 and is unable to scan the final int 297. The whole scanning process is in a try catch block which calls the parseCSVLine method. – andrewkeithly Oct 22 '15 at 20:19
  • does it work if you use next()? – AbtPst Oct 22 '15 at 20:39
  • Nope, that was the first thing I tried. I think it has to do with the end of line delimiter. – andrewkeithly Oct 22 '15 at 20:48
  • @andrewkeithly welcome to SO. Any time you are looking for help on an exception it is useful to post the stack trace and corresponding line in your code sample. – Martin Serrano Oct 22 '15 at 21:12
  • Thanks @MartinSerrano, I've posted the full code along with error. – andrewkeithly Oct 22 '15 at 22:00
  • Can you provide an example file content which causes the exception? And can you please post the _whole_ exception? – Tom Oct 22 '15 at 22:17
  • @Tom I've updated the code to better illustrate my issue. – andrewkeithly Oct 22 '15 at 22:25
  • Your code works fine for me _if_ the file is written with the correct line delimiter. Either the file was written "for" Unix and you're on Windows or vice versa. – Tom Oct 22 '15 at 22:37
  • Thanks @Tom, I think you may be correct. I'm on a Unix machine and the csv was likely written "for" Windows. I'm investigating now. – andrewkeithly Oct 22 '15 at 22:45
  • Wow. What a silly error… I was using System.getProperty("line.separator") which was referencing a specific Unix delimiter. Problem solved! – andrewkeithly Oct 22 '15 at 22:53

1 Answers1

4

Problem Solved: System.getProperty("line.separator") was looking for a Unix specific delimiter in which the csv did not use. Simply changing the code to scanner.useDelimiter("\r?\n"); solved my problem. Thanks @Tom !

andrewkeithly
  • 133
  • 11
  • Please don't use `useDelimiter(" ")` which looks like it will cause more trouble. Use `useDelimiter("\r?\n")` instead. It reads `"\r"` if it is there and it definitely reads `"\n"`, so works for both, Unix and Windows. – Tom Oct 22 '15 at 23:04