從文本文件驗證數據時遇到問題。我只能驗證我的身份證號碼,只有其他字段無效。從文本文件讀取數據並使用Java進行驗證
下面是我的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);
PLZ告訴我們您的文本文件的內容也 – Joe 2014-12-07 01:55:21
@johny這是它顯示的內容: 001#瓊斯#傑克#41#NX4 4XZ# – 2014-12-07 02:07:06