Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java扫描程序分隔符错误csv Java行结束_Java_Csv_Delimiter_Inputmismatchexception - Fatal编程技术网

Java扫描程序分隔符错误csv Java行结束

Java扫描程序分隔符错误csv Java行结束,java,csv,delimiter,inputmismatchexception,Java,Csv,Delimiter,Inputmismatchexception,扫描csv的最后一个整数时,我收到一个输入不匹配异常 public class TradeSim { public void readFile(String fileName) { try { // file name and absolute path File file = new File(fileName); String fullPath = file.getAbsolutePath();

扫描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);
    }
}
文件内容:

fbc,51300051409,273297
51300073658,cef,250262

输出:

使用文件:
/用户/andrewkeithly/netbeansprojects/Trades Exercise/input.csv

fbc,51300051409,273297
时间戳:5130051409
符号:fbc
数量:273
读取文件时遇到错误:
java.util.InputMismatchException


问题已解决:
System.getProperty(“line.separator”)
正在查找csv未使用的Unix特定分隔符。
只需将代码更改为
scanner.useDelimiter(“\r?\n”)解决了我的问题。谢谢@Tom

问题已解决:
System.getProperty(“line.separator”)
正在查找csv未使用的Unix特定分隔符。
只需将代码更改为
scanner.useDelimiter(“\r?\n”)解决了我的问题。谢谢@Tom

也许这个位置不是整数。首先在try-catch块中包含整个扫描过程,最好使用scanner.next()。稍后您可以将其解析为needed@AbtPst谢谢你的回复!它读取的行是:它扫描前3个,但无法扫描最后的int 297。整个扫描过程都在一个try-catch块中,该块调用parseCSVLine方法。如果使用next(),它能工作吗?不,这是我尝试的第一件事。“我认为这与行尾分隔符有关。”安德鲁克表示欢迎。任何时候当你在寻找关于异常的帮助时,在你的代码样本中发布堆栈跟踪和相应的行是很有用的。可能在那个位置不是int。首先在try-catch块中包含整个扫描过程,最好使用scanner.next()。稍后您可以将其解析为needed@AbtPst谢谢你的回复!它读取的行是:它扫描前3个,但无法扫描最后的int 297。整个扫描过程都在一个try-catch块中,该块调用parseCSVLine方法。如果使用next(),它能工作吗?不,这是我尝试的第一件事。“我认为这与行尾分隔符有关。”安德鲁克表示欢迎。任何时候当您在寻找有关异常的帮助时,都可以在代码示例中发布堆栈跟踪和相应的行。请不要使用
useDelimiter(“”)
,这可能会导致更多问题。改用
使用分隔符(\r?\n”)
。如果它在那里,它会读
“\r”
,而且它肯定会读
“\n”
,所以在Unix和Windows上都适用。请不要使用
使用分隔符(“”
),这看起来会造成更多的麻烦。改用
使用分隔符(\r?\n”)
。如果它在那里,它会读
“\r”
,而且它肯定会读
“\n”
,所以它对Unix和Windows都有效。