Is it possible to try/catch
continue doing instructions, even if firts answer is wrong?
For example in the code, if I just type hello
instead of true
or false
, it just goes to the catch part exception. However it is not still running with all other questions. How can I get the program to execute each question, even if one of these throw the InputMismatchException
due to a wrong input?
import java.util.InputMismatchException;
import java.util.Scanner;
public class DataTypeJava {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
try {
System.out.println("Escribe un dato de tipo boolean opciones son (true/false: ");
boolean booleanType = scan.nextBoolean();
System.out.println("Tipo Booleado: " + booleanType);
System.out.print("Escribe un tipo de dato byte : ");
byte byteType = scan.nextByte();
System.out.println("Tipo Byte: " + byteType);
System.out.print("Escribe un dato de tipo char: ");
String charType = scan.next();
System.out.println("Tipo Char: " + charType);
System.out.print("Escribe un dato de tipo short: ");
short shortType = scan.nextShort();
System.out.println("Tipo Short: " + shortType);
System.out.print("Escribe un dato de tipo int: ");
int intType = scan.nextInt();
System.out.println("Tipo Int: " + intType);
System.out.print("Escribe un dato de tipo long: ");
long longType = scan.nextLong();
System.out.println("Tipo Long: " + longType);
System.out.print("Escribe un dato de tipo float: ");
float floatType = scan.nextFloat();
System.out.println("Tipo Float: " + floatType);
System.out.print("Escribe un dato de tipo double ");
double doubleType = scan.nextDouble();
System.out.println("Tipo Double: " + doubleType);
} catch (InputMismatchException e) {
//TODO: handle exception
System.out.println("Dato ingresado no es correcto");
}
}
}