-1

So, I've searched around google and stackoverflow for a bit, but I can't seem to find an answer to this issue.

I've got 2 methods. The first method, getAge is meant to just get an integer as input from the user. It's then meant to pass that input to verifyAge, who makes sure it's in the right range.

However; if they should enter anything that is not an integer, it's supposed to display a message and call getAge again, to restart the input process. I have a try-catch set up, but it still goes back to JVM. According to an answer in another post; what I'm doing is correct. But it still doesn't seem to be working. So here's the errors I get when I try to run it as I have it now:

Please enter your age: notint
Exception in thread "main" java.util.InputMismatchException
 at java.util.Scanner.throwFor(Scanner.java:864)
 at java.util.Scanner.next(Scanner.java:1485)
 at java.util.Scanner.nextInt(Scanner.java:2117)
 at java.util.Scanner.nextInt(Scanner.java:2076)
 at Ch2ProgLabWilson.getAge(Ch2ProgLabWilson.java:22)
 at Ch2ProgLabWilson.main(Ch2ProgLabWilson.java:15)

What I have written:

import java.util.* ;
import java.util.Scanner;

public class Ch2ProgLabWilson {

 public static void main(String[] args) {

      getAge();  
 }

 public static int getAge()
 {
  Scanner keyboard = new Scanner(System.in);
  System.out.print("Please enter your age: ");
  int a = keyboard.nextInt();
  verifyAge(a);

     try
     {
        getAge();
     }    
     catch (InputMismatchException e)
        {
           System.out.println("You may only enter integers as an age. Try again.");
           getAge();
        }

    return a;
   }

 // 
 public static boolean verifyAge (int a)
     {
        if (a >= 0 && a <= 122)
        {
           System.out.println("The age you entered, " + a + ", is valid.");
           return true;
        }
        else
        {
           System.out.println("The age must be from 0 to 122, cannot be negative, and has to be an integer.");
           getAge();
           return false;
        }   
     }

 }
Will Troll
  • 23
  • 1
  • 1
  • 3

3 Answers3

3

The Exception is being thrown by int a = keyboard.nextInt();, which is outside of the try catch block. Place the call to int a = keyboard.nextInt(); inside the try block.

There are other problems with your code:

verifyAge() returns a boolean that is never used.

Your getAge() method is recursive and, assuming that the user enters a number, it will just loop - is this what you intended?

UPDATE

public static int getAge(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter your age: ");
    int age = -1;

    while(!verifyAge(age)){ // will loop until there's a valid age
        try{
            age = scanner.nextInt();
        catch (InputMismatchException e){
            System.out.println("You may only enter integers as an age. Try again.");
        }
    }

    return age; // your current code doesn't do anything with this return value
}

public static boolean verifyAge (int a){ // would be better named isValidAge()
    if (a >= 0 && a <= 122){
        System.out.println("The age you entered, " + a + ", is valid.");
        return true;
    }else{ // no need to call getAge() here
       System.out.println("The age must be from 0 to 122, cannot be negative, and has to be an integer.");
       return false;
    }   
 }
Romski
  • 1,912
  • 1
  • 12
  • 27
  • The problem with puting int a = keyboard.nextInt(); is that at the end of getAge(), it requests a return statement. However; it doesn't recognize variable "a" if I have "return a;" there. getAge() is meant to just get the integer from the user and pass it to verify age. Once it's verified, it'll respond appropriately in verifyAge(). – Will Troll Sep 08 '14 at 08:11
  • @WillTroll What should `getAge()` return if `nextInt()` throws an exception? – Code-Apprentice Sep 08 '14 at 08:28
  • @will The method will never return as you call `getAge()` in both the try and catch. If the goal is to keep looping until a valid age is entered, then I would use `verifyAge()` to control a while loop until a valid age is entered - I've edited the answer to demonstrate – Romski Sep 08 '14 at 12:47
2

well the code is not within the try block

  Scanner keyboard = new Scanner(System.in);
  System.out.print("Please enter your age: ");
  int a = keyboard.nextInt();
  verifyAge(a);

  try
  {

so it will not get caught.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

Look at the line of code which actually throws the Exception. If it is not in the try/catch block, it will not be caught.

Try this instead:

try
{
    int a = keyboard.nextInt();
    verifyAge(a);
}    
catch (InputMismatchException e)
{
    System.out.println("You may only enter integers as an age. Try again.");
    getAge();
}
PKlumpp
  • 4,913
  • 8
  • 36
  • 64