raz
raz

Reputation: 128

Beginner Java program

I'm a beginner with Java and I have the following code:

public class test
{
   public static void main(String[] args) 
   {
      int a ;
      String b;

      Scanner input = new Scanner(System.in);
      try {

        System.out.println("Enter a: ");
        a = input.nextInt();

    } catch(Exception e )
    {
        System.out.println("That is not a number!");
    }

      System.out.println("Enter your name: ");
      b = input.next();
      System.out.println("Hello " + b);

    }
}

when I give a string instead of int the code performs further and I receive this result:

"Enter a:
fsd
That is not a number!
Enter your name: 
Hello fsd"

How can I make an interruption after the catch? (I have already try with a new Scanner after catch, but I think there are also another ways) Thanks in advance!

LE: I have managed to do that with the "input.next();". I actually wanted to tip in another value for the string b, but the program takes automatically the int a instead of a new value and prints "Hello vsd", although vsd was the input for a.

Upvotes: 2

Views: 384

Answers (4)

Bruce219
Bruce219

Reputation: 39

You should try to use the following block

input.hasNextInt()

instead as it doesn't require you to use a catch or throw exception, but rather if and else statements.

Your code still carries on because the catch block simply catches the exception and replaces it with a System.out.println that prints "That is not a number!"

   } catch(Exception e )
{
    System.out.println("That is not a number!");
}

Upon doing so the code continues to

 System.out.println("Enter your name: ");
 b = input.next();
 System.out.println("Hello " + b);

Upvotes: 0

Tarek
Tarek

Reputation: 771

Just modify your catch block as follow:

catch(Exception e )
     {
        System.out.println("That is not a number!");
        System.exit(0);
     }

Upvotes: 0

Mureinik
Mureinik

Reputation: 311843

As stated by the documentation, the Scanner will not advance to the next token if the current token does not match an integer's format. Therefore, if an exception is not thrown, the token in not consumed, and you just get it the next time you call a nextXYZ method.

Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.

Instead of throwing and catching the exception (which is a heavy operation), you could use hasNextInt() to check it in advance:

int a ;
String b;

Scanner input = new Scanner(System.in);
System.out.println("Enter a: ");
if (input.hasNextInt()) {
    a = input.nextInt();
} else {
    System.out.println("That is not a number!");
    // Skip the token so you won't get it later:
    input.nextLine();
}

System.out.println("Enter your name: ");
b = input.next();
System.out.println("Hello " + b);

Upvotes: 2

Muhammad Shuja
Muhammad Shuja

Reputation: 672

Try this

public class Test{
public static void main(String[] args){
  int a ;
  String b;
  Scanner input = new Scanner(System.in);
  try {

    System.out.println("Enter a: ");
        a = input.nextInt();
    System.out.println("Enter your name: ");
        b = input.next();
    System.out.println("Hello " + b);

    }
  catch(Exception e ){
    System.out.println("That is not a number!");
    }
}
}

Upvotes: 1

Related Questions