2014-12-07 57 views
0

從文本文件驗證數據時遇到問題。我只能驗證我的身份證號碼,只有其他字段無效。從文本文件讀取數據並使用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); 
+0

PLZ告訴我們您的文本文件的內容也 – Joe 2014-12-07 01:55:21

+0

@johny這是它顯示的內容: 001#瓊斯#傑克#41#NX4 4XZ# – 2014-12-07 02:07:06

回答

0

不要scnr.next()混合scnr.nextInt()。改爲使用I'd = Integer.parseInt(scnr.next());。很抱歉,如果不是這種情況

+0

我的意思是使用'ID = Integer.parseInt(scnr.next());'手機上的自動更正有時可能真的很煩人:) – oxyt 2014-12-07 02:05:18

+0

它似乎不驗證其他字段,如名和姓.. – 2014-12-07 02:07:51

+0

更改'while(scnr.hasNextInt ())''while while(scnr.hasNext())'' – oxyt 2014-12-07 02:09:01