Java 扫描仪定界符错误 csv java 行尾
Java scanner delimiter error csv java end of line
我在扫描 csv 的最后一个 int 时收到 InputMismatchException
。
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);
}
}
文件内容:
51300051409,fbc,273,297
51300073658,cef,250,262
输出:
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
已解决的问题:
System.getProperty("line.separator")
正在寻找 csv 未使用的特定于 Unix 的分隔符。
只需将代码更改为 scanner.useDelimiter("\r?\n");
即可解决我的问题。谢谢@汤姆!
我在扫描 csv 的最后一个 int 时收到 InputMismatchException
。
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);
}
}
文件内容:
51300051409,fbc,273,297
51300073658,cef,250,262
输出:
Using file:
/Users/andrewkeithly/netbeansprojects/Trades Exercise/input.csv51300051409,fbc,273,297
timeStamp: 51300051409
symbol: fbc
quantity: 273
Encountered Error reading file:
java.util.InputMismatchException
已解决的问题:
System.getProperty("line.separator")
正在寻找 csv 未使用的特定于 Unix 的分隔符。
只需将代码更改为 scanner.useDelimiter("\r?\n");
即可解决我的问题。谢谢@汤姆!