2011-02-19 32 views
0

希望你能幫我編碼。地址簿中的邏輯問題,應該沒有空白條目或相同的條目...請幫助

我需要做什麼: 1)我不應該讓用戶有一個空白項。我要提醒他們像「沒有輸入名字!」 2)我不應該允許用戶輸入同一條目..

我不知道怎麼我的代碼,請大家幫忙。

這裏是我的完整代碼:

公共類通訊錄{

private AddressBookEntry entry[]; 
private int counter; 
private String SName; 
private int notfound=0; 

public static void main(String[] args) { 
    AddressBook a = new AddressBook(); 
    a.entry = new AddressBookEntry[100]; 
    int option = 0; 
    while (option != 5) { 
     String content = "Choose an Option\n\n" 
       + "[1] Add an Entry\n" 
       + "[2] Delete an Entry\n" 
       + "[3] Update an Entry\n" 
       + "[4] View all Entries\n" 
       + "[5] View Specific Entry\n" 
       + "[6] Exit"; 
     option = Integer.parseInt(JOptionPane.showInputDialog(content)); 
     switch (option) { 
      case 1: 
       a.addEntry(); 
       break; 
      case 2: 
       a.deleteEntry(); 
       break; 
      case 3: 
       a.editEntry(); 
       break; 
      case 4: 
       a.viewAll(); 
       break; 
      case 5: 
       a.searchEntry(); 
       break; 
      case 6: 
       System.exit(1); 
       break; 
      default: 
       JOptionPane.showMessageDialog(null, "Invalid Choice!"); 
     } 
    } 
} 

public void addEntry() { 
    entry[counter] = new AddressBookEntry(); 
    entry[counter].setName(JOptionPane.showInputDialog("Enter name: ")); 
    entry[counter].setAdd(JOptionPane.showInputDialog("Enter add: ")); 
    entry[counter].setPhoneNo(JOptionPane.showInputDialog("Enter Phone No.: ")); 
    entry[counter].setEmail(JOptionPane.showInputDialog("Enter E-mail: ")); 
    counter++; 
} 

public void viewAll() { 
    String addText = " NAME\tADDRESS\tPHONE NO.\tE-MAIL ADD\n\n"; 
    int nonNull = 0; 
    for (int i = 0; i < entry.length; i++) { 
     if (entry[i] != null) { 
      addText = addText + entry[i].getInfo() + "\n"; 
      nonNull++; 
     } 
     if (nonNull == counter) { 
      break; 
     } 
    } 
    JOptionPane.showMessageDialog(null, new JTextArea(addText)); 
} 

public void searchEntry() { 
    SName = JOptionPane.showInputDialog("Enter Name to find: "); 
    searchMethod(); 
} 

public void searchMethod() { 
    for (int i = 0; i < counter; i++) { 
     if (entry[i].getName().equals(SName)) { 
      JOptionPane.showMessageDialog(null, entry[i].getInfo2()); 
      notfound = 0; 
      break; 
     } else { 
      notfound++; 
     } 
    } 
    if (notfound != 0) { 
     JOptionPane.showMessageDialog(null, "Name Not Found!"); 
    } 
} 
public void editEntry() { 
    int notfound = 0; 
    SName = JOptionPane.showInputDialog("Enter Name to edit: "); 
    for (int i = 0; i < counter; i++) { 
     if (entry[i].getName().equals(SName)) { 
      entry[i] = new AddressBookEntry(); 
      entry[i].setName(JOptionPane.showInputDialog("Enter new name: ")); 
      entry[i].setAdd(JOptionPane.showInputDialog("Enter new add: ")); 
      entry[i].setPhoneNo(JOptionPane.showInputDialog("Enter new Phone No.: ")); 
      entry[i].setEmail(JOptionPane.showInputDialog("Enter new E-mail: ")); 
      notfound = 0; 
      break; 
     } else { 
      notfound++; 
     } 
    } 
    if (notfound != 0) { 
     JOptionPane.showMessageDialog(null, "Name Not Found!"); 
    } 
} 
public void deleteEntry() { 
    SName = JOptionPane.showInputDialog("Enter Name to delete: "); 
    for (int i = 0; i < counter; i++) { 
     if (entry[i].getName().equals(SName)) { 
      JOptionPane.showMessageDialog(null, "Found!"); 
      entry[i] = null; 
      break; 
     } 
    } 
} 

}

什麼發生在我的代碼是它允許用戶點擊「OK」即使他不參加任何性格又..它也允許相同的條目...

希望你能幫助我......我很快需要它,但我仍然不知道在我的代碼添加...請^ h ELP非常感謝..

+0

如果您正在使用反正JOptionPane的,你應該看看`showOptionDialog`它允許你顯示按鈕,而不是輸入行的列表。 – 2011-02-19 02:43:23

+0

@Sir Paulo ...美好的一天...我是新來的java和不熟悉的內置函數...我會嘗試使用這個謝謝:) – iamanapprentice 2011-02-19 02:46:13

回答

2

什麼發生在我的代碼是它允許用戶點擊「OK」,甚至他不輸入任何字符尚未..它也允許相同的條目...

showInputDialog方法讓用戶輸入他想要的任何字符串,或者什麼都不是。如果你不想這樣,你可以使用一個自制的對話框,修補的JOptionPane(使用子類,或者這樣的),或者乾脆再次顯示該對話框時,這是不是你給的一個字符串,像這樣:

String input = JOptionPane.showInputDialog(content); 
try { 
    option = Integer.parseInt(input); 
} 
catch(NumberFormatException ex) { 
    // the user typed nothing, or something that is not an integer 
    // TODO: show error message 
    // => go to next iteration of while loop. 
    continue; 
} 
// and now our switch ...