java - How to use if statement to print a message when having a mismatch error instead of showing the compile error message? -
i tried create if statement within code print message if user entered wrong type of input rather showing "inputmismatchexception" message compiler.
import java.util.*; public class pr8 { public static void main(string[] args) { scanner scan = new scanner(system.in); //prompt user how many integers going entered system.out.print("*please write how many numbers going entered: "); int = scan.nextint(); int[] n = new int[a]; if(a >= 0) for(int = 0; < a; i++) { system.out.print("*please come in enteger: "); n[i] = scan.nextint(); }//for else system.out.print("*sorry entery not correct. please come in digits only. "); }//main }//pr8
check scan.hasnextint()
, work you.
public static void main(string[] args) { scanner scan = new scanner(system.in); // prompt user how many integers going entered system.out.print("*please write how many numbers going entered: "); if (scan.hasnextint()) { int = scan.nextint(); int[] n = new int[a]; (int = 0 ; < ; i++) { system.out.print("*please come in enteger: "); if (scan.hasnextint()) { n[i] = scan.nextint(); } else { system.out.print("*sorry entery not correct. please come in digits only. "); break; } }// } else { system.out.print("*sorry entery not correct. please come in digits only. "); } }
java type-mismatch
No comments:
Post a Comment