Results 1 to 2 of 2
  1. #1
    Bicentric is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default Exception Handling

    So a while back I learnt about exception handling in Java, but only now I have put it to use. Here is my code, I am catching an exception where the user may input a incorrect character.

    Java Code:
    import java.util.InputMismatchException;
    import java.util.Random;
    import java.util.Scanner;
    
    public class GuessTheNumber 
    {
    	public static int randomNumber;
    	public static int guessedNumber;
    	
    	static Random rand = new Random();
    	static Scanner input = new Scanner(System.in);
    	
    	public static void main(String args[])
    	{
    		randomNumber = rand.nextInt(100);
    		update();
    	}
    	
    	public static void update()
    	{
    		guessedNumber = getInput();
    		checkInput();	
    	}
    	
    	public static int getInput()
    	{
    		guessedNumber = 0;
    		int returnNum = 0;
    		System.out.println("Enter a number between 1-100!");
    		try
    		{
    			returnNum = input.nextInt();
    		}
    		catch(InputMismatchException e)
    		{
    			System.out.println("There was an exception: " + e);
    			getInput();
    		}
    		return returnNum;
    	}
    	
    	public static void checkInput()
    	{
    		if(guessedNumber == randomNumber)
    		{
    			System.out.println("Correct number! You win the game!");
    			System.exit(0);
    		}
    		else if(guessedNumber < 0 || guessedNumber > 100)
    		{
    			System.out.println("What are you playing at?");
    		}
    		else if(guessedNumber > randomNumber)
    		{
    			System.out.println("A little lower.");
    		}
    		else if(guessedNumber < randomNumber)
    		{
    			System.out.println("A little higher.");
    		}
    		update();
    	}
    }
    And here is my output when I enter an incorrect character, it just spams me with this, and the program exits.

    Since the output is too big, I have put it on Pastebin (Click Me)

    Please could anyone tell me what I'm doing wrong?

    Regards,
    - Bicentric

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Exception Handling

    You should write a better code.

    Fast solution for one part of your program problems is:

    Java Code:
    System.out.println("Enter a number between 0 and 100");
    		
    		try {
    			returnNum = input.nextInt();
    		} catch(InputMismatchException e) {
    			System.out.println("Warning: Only allowed characters are 0-9");
    			System.exit(0);
    			//getInput();
    		}
    		
    		return returnNum;
    But my advice for you is that you should write your program again. It's bad practice to call your method getInput() inside same method (recursion) when you can use for example for or while loop for calling your method. It's faster, it's better, code are more elegant.

Similar Threads

  1. Exception handling
    By Kartiky14 in forum New To Java
    Replies: 3
    Last Post: 03-25-2012, 09:07 AM
  2. Exception handling
    By ExX in forum New To Java
    Replies: 6
    Last Post: 03-12-2012, 12:48 PM
  3. Exception handling
    By Moedig in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 11:43 PM
  4. Exception handling
    By herat in forum New To Java
    Replies: 1
    Last Post: 06-21-2011, 11:13 AM
  5. Exception Handling
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-19-2011, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •