java - Preventing the user from entering identical elements(within a malfunctioning try catch) -


so following code simple game,where objective to guess correct numbers(which 1 5).anything else incorrect , user given warning message if enter similar numbers.the comments explain loops , variables declared.

the problem have code inserted try catch take care of strings , doesn't seem work.if string entered,the while loop continues infinitely.

also,i realize there loop pf looping , conditional statements present in code,but couldn't think of else.if have recommendations reduce number of loops , if statements,your appreciated.

 public class tries {             public static void main(string[]args)             {               boolean datatype=false;             int inp;             scanner a=new scanner(system.in);     //the arraylist,list, contains input user enters.only correct input entered(1 5).             arraylist<integer> list=new arraylist<integer>();      //this determines how many times loop going execute.say user enters 4,and enters 4 correct inputs,the program exit.the variable num determines size of arraylist list going be.                 system.out.println("how many tries?");                 int num=a.nextint();                   boolean datatype=false;                      for(int j=0;j<num;j++)                     {     //this while loop try catch.                         while(!datatype)                         {                          scanner sc=new scanner(system.in);      //this while loop ensures user re enters input when other correct numbers entered.                         while(list.size()!=num)                         {                             try                             {                                 system.out.println("\npick number: ");                                 inp=sc.nextint();                              if(inp==1 || inp==2 || inp==3 || inp==4 || inp==5)                             {                                  datatype=true;                                 system.out.println(j);                                  if(list.size()==0)                                 {                                     list.add(inp);                                 }                                 else if(list.size()>0)                                 {                                         if(list.contains(inp))                                         {                                             system.out.println("already entered.try again.");                                            }                                         else if(!list.contains(inp))                                             {                                             list.add(inp);                                             system.out.println("added");                                             datatype=true;                                              system.out.println(list);                                         }                                  }                             }                                 else                                 {                                     system.out.println("option not available.");                                     datatype=false;                                 }                              }                             catch(exception javainputmismatch)                             {                                 system.out.println("option not available.try again.");                                 datatype=false;                             }                         }                     }                 }             }         } 

so, when inp=sc.nextint(); fails because user enters invalid number, inputmismatchexception gets thrown. loop again, , attempt run inp=sc.nextint(); again.

the problem though invalid number entered still in input stream waiting read. in next loop, when inp=sc.nextint(); attempted again, doesn't try read in new value, reads previous invalid value without allowing type new. , keeps happening on , on indefinitely.

the quick fix? need clear out input stream rid of invalid number before attempting read new one.

the simplest way plug fix in program adding sc.next(); call in catch block this:

catch(exception javainputmismatch) {     sc.next(); // clear bad token. without this, loops infinitely.     system.out.println("option not available.try again.");     datatype=false; } 

there quite few other changes/improvements make program, i'll admit lack motivation @ moment address those. @ least unblock you.

edit: guess can add few high level suggestions can you:

  • as commented, shouldn't have 2 scanner instances reading system.in.
  • i recommend dropping whole try-catch detect invalid number, , instead use sc.hasnextint() check before reading number sc.nextint(). if did keep catch block, recommend make exception type specific possible (e.g. catch(inputmismatchexception e)) instead of catch-all exception. otherwise, risk catching irrelevant exceptions , handling them wrong way.
  • you should able drop datatype boolean variable , associated loop. it's enough looping long list not full.
  • in fact, if i'm understanding correctly, can simplify loops keeping 1 while(list.size()!=num). think can safely rid of loop for(int j=0;j<num;j++).
  • minor detail, can express if(inp==1 || inp==2 || inp==3 || inp==4 || inp==5) more succinctly instead: if(inp >= 1 && inp <= 5).
  • and finally, logic determines whether add number list or not doesn't need bunch of conditions based on size of list.

something sufficient:

if (list.contains(inp)) {     system.out.println("already entered.try again."); } else {     list.add(inp);     system.out.println("added");     system.out.println(list); } 

i hope helps.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -