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 an integer number");
AreaofCircle a = new AreaofCircle();
}
}
}
Here in this code , assume user enter some string using keyboard, at that point in try block it shows exception and it comes to catch block,
And i want to show the details of error and want to push back user to that code again, how this is 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 an integer number");
allowedAttempts++;
if(allowedAttempts==3){
System.out.println("No More attempts allowed");break;
}
}
}
You can use integer variable, and prompt upto specific number of attempts.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.