is there any Exception in Java that to detect no input? -


what exception need add try catch block if want detect if user has entered characters?

this code want know if user hasn't input or if user's input saved in addressbook. using array store entries:

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++; } 

do need try-catch or condition? here's complete code:

public class addressbook {  private addressbookentry entry[]; private int counter; private string sname; private int notfound = 0;  public static void main(string[] args) {     addressbook = new addressbook();     a.entry = new addressbookentry[100];     int option = 0;     try {         while (option != 5) {             string content = "choose option\n\n"                     + "[1] add entry\n"                     + "[2] delete entry\n"                     + "[3] update entry\n"                     + "[4] view 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!");             }         }     }catch(numberformatexception e){         joptionpane.showmessagedialog(null, "please choose number in displayed menu");     } }  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;     (int = 0; < 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 find: ");     searchmethod(); }  public void searchmethod() {     (int = 0; < 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() {     sname = joptionpane.showinputdialog("enter name edit: ");     (int = 0; < 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 delete: ");     (int = 0; < counter; i++) {         if (entry[i].getname().equals(sname)) {             joptionpane.showmessagedialog(null, "found!");             entry[i] = null;             break;         }     } } 

}

i having problem addentry() method, because want detect if user's new added entry stored in addressbook , if user doesn't typed when ask "enter name:" in joptionpane , still press ok.

from updated question, appears if talking user input dialog boxes. javadoc says showinputdialog methods return either user's string (which empty) or null if user canceled dialog.

no exception thrown. user canceling dialog not "exceptional".


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -