java - how to call the class again if the user enter wrong input using keyboard -
java - how to call the class again if the user enter wrong input using keyboard -
package simple; import java.io.bufferedreader; import java.io.inputstreamreader; class areaofcircle{ public static void main(string args[]){ float pi = 3.1416f; int r=0; string rad; bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); system.out.println("radius?"); try{ rad = br.readline(); r = integer.parseint(rad); system.out.println("circle area is: " + pi*r*r + " permieter: " +pi*2*r); } catch(exception e){ system.out.println("write integer number"); areaofcircle = new areaofcircle(); } } }
here in code , assume user come in string using keyboard, @ point in seek block shows exception , comes grab block,
and want show details of error , want force user code again, how possible, plz help me regarding this.
int allowedattempts=0; while(true) { try{ rad = br.readline(); r = integer.parseint(rad); system.out.println("circle area is: " + pi*r*r + " permieter: " +pi*2*r); break; } catch(exception e){ system.out.println("write integer number"); allowedattempts++; if(allowedattempts==3){ system.out.println("no more attempts allowed");break; } } }
you can utilize integer variable, , prompt upto specific number of attempts.
java input exception-handling output core
Comments
Post a Comment