我在验证文本文件中的数据时遇到了问题。我只能验证我的ID号,只有其他字段不起作用。
以下是我的Java代码:
public void readBorrowerData() {
String fnm="", snm="", pcd="";
int num=0, id=1;
try {
Scanner scnr = new Scanner(new File("borrowers.txt"));
scnr.useDelimiter("\\s*#\\s*");
if(id < 0 || snm == null || fnm == null || num < 0){
JOptionPane.showMessageDialog(this, "Try again", "Error", JOptionPane.ERROR_MESSAGE);
} else {
while (scnr.hasNextInt()) {
id = scnr.nextInt();
snm = scnr.next();
fnm = scnr.next();
num = scnr.nextInt();
pcd = scnr.next();
borrowers.put(new Integer(id), new Borrower(id, snm, fnm, num, pcd));
}
}
scnr.close();
} catch (NoSuchElementException e) {
System.out.printf("%d %s %s %d %s\n", id, snm, fnm, num, pcd);
JOptionPane.showMessageDialog(this, e.getMessage(),
"fetch of next token failed ", JOptionPane.ERROR_MESSAGE);
答案 0 :(得分:0)
请勿将scnr.nextInt()
与scnr.next()
混合使用。请改用I'd = Integer.parseInt(scnr.next());
。对不起,如果不是这样的话