Hello, i've been messing around with basic scripting in java, and now i'm trying to get exceptions down. Here is the code:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package First; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author Office */ public class Constructorexample { public int total; public static int yourage; public static String yourname; static Scanner s = new Scanner(System.in); public static void getnameandage(String name, int age){ int l = name.length(); int ax2 = age*2; System.out.println(); System.out.println("The total length of your name is " + l); System.out.println(); System.out.println("Your age x 2 = " + ax2); } public static void main(String[] args) { System.out.println("Please enter your name:"); try{ yourname = s.nextLine(); }catch (Exception e){ System.out.println("Wrong input!"); } System.out.println(); System.out.println("Now please enter your age:"); yourage = s.nextInt(); getnameandage(yourname, yourage); } }
I want to check if the user input a number instead of letters, and vice verse for the second prompt. I've tryed a try/catch block around yourname=s.nextLine(); and this stop the program from stopping, but it wont display text like I ask it to. Could someone help me out here? I also would like the program to ask the question again once the error is displayed, how would I do that? Thanks for any help!