mercredi 11 mars 2020

Input numeric to proceed to next line but exception when entering a non numeric

I am new to programming and am still wrapping my head around things.

So pretty much, I am trying to add a line that will allow entering, for example, a text "ABC", and for it not to produce an exception but rather request for a numeric value be entered "123".

As i said i am new to programming and I have tried many attempts, but i think because im using "int age = input.nextInt();" it stops me entering text in the if statement. Im not really sure.

This is for an assignment as well. Let me know if you need to know anything more. I do want to learn, though i also dont know what to look at. etc.

   System.out.println("Please enter your age:");

    String bracket= "";

     int age = input.nextInt();

      if (age <20){
        bracket = "20 & under";
        }
       if (age >20 && age <36){
        bracket = "21-35";
        }
        if (age >35 && age <71){
        bracket = "36-70";
        }
         if (age >70){
         bracket = "71 & over";
         }

Aucun commentaire:

Enregistrer un commentaire