Using Try/Catch in JAVA -


i have following piece of code i'm trying user enter integers; if string entered display system out error message "please enter numbers" , show "enter id#:" again. tried using try/catch method not using correctly -- still beginner. know can use "numberformatexception" not sure where. can help? thanks!

        //get customer id , account number                 {   system.out.print("enter id#: ");             custid = integer.parseint(input.readline());             system.out.print("enter account number#: ");             custacctnum = integer.parseint(input.readline());         //validate choice             for(int i=0; i<people.length; i++)               {   if ((people[i].custid == custid) && (people[i].custacctnum == custacctnum))                 {   match = true;                 system.out.println("welcome "  +people[i].firstname+ " jj dealership!");                 for(int p=0; p<cluster.length; p++)                     system.out.println(+(p+1)+ ": " +cluster[p].year+"," +cluster[p].make+ "," +cluster[p].model);                  system.out.println(people[i].firstname+ ", color car like?");                     break;                 }             }             if (!match)             {   system.out.println("invalid id");            } while (!(match)); 

exceptions should exceptional circumstances. suggest use scanner , hasnextint() continue when isn't int. make scanner like,

scanner input = new scanner(system.in); 

and work,

do {     system.out.print("enter id#: ");     if (!input.hasnextint()) {         system.out.printf("%s not int.%n", input.nextline());         continue;     }     custid = input.nextint();     system.out.print("enter account number#: ");     custacctnum = input.nextint();     if (!input.hasnextint()) {         system.out.printf("%s not int.%n", input.nextline());         continue;     } 

if want use try-catch should like,

do {     try {         system.out.print("enter id#: ");         custid = integer.parseint(input.readline());         system.out.print("enter account number#: ");         custacctnum = integer.parseint(input.readline());     //validate choice         for(int i=0; i<people.length; i++)           {   if ((people[i].custid == custid) &&                     (people[i].custacctnum == custacctnum))             {   match = true;             system.out.println("welcome "  +people[i].firstname                     + " jj dealership!");             for(int p=0; p<cluster.length; p++)                 system.out.println("" + (p+1) + ": " +cluster[p].year+","                         + cluster[p].make+ "," +cluster[p].model);              system.out.println(people[i].firstname                      + ", color car like?");                 break;             }         }     } catch (numberformatexception nfe) {         nfe.printstacktrace();     } } while (!(match)); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -