Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By wsaryada

Thread: How do I apply exception handling (my error-proofing adventure inside)

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default How do I apply exception handling (my error-proofing adventure inside)

    Ok so this is my original code

    Java Code:
    public void setPlayerSymbol()
    	{
    		int playerChoice;
    		
    		System.out.println("What symbol will player one use? (1 = O, 2 = X)");
    		playerChoice = input.nextInt();
    		
    		if( playerChoice == 1 )
    		{
    			player1 = value.O;
    			player2 = value.X;
    		}
    		else // player == 2
    		{
    			player1 = value.X;
    			player2 = value.O;
    		}
    	}
    Hold on here! what if somebody picks a number that is not 1 or 2?
    well, lets make this robust! next attempt

    Java Code:
    int playerChoice;
    			boolean reselect;
    
    			System.out.println("What symbol will player one use? (1 = O, 2 = X)");
    			
    			do{
    				
    				reselect = false;
    				playerChoice = input.nextInt();
    
    				if( playerChoice == 1 )
    				{
    					player1 = value.O;
    					player2 = value.X;
    				}
    				else if ( playerChoice == 2 )
    				{
    					player1 = value.X;
    					player2 = value.O;
    				}
    				else
    				{
    					System.out.println("Invalid selection, please select again");
    					reselect = true;
    				}
    			}
    
    			while( reselect == true );
    hold on... what if somebody doesnt understand the program and types "I love cookies"? could happen... then I'd be in trouble. next try!

    Java Code:
    	public void setPlayerSymbol()
    	{
    
    		try
    		{
    			int playerChoice;
    			boolean reselect;
    
    			System.out.println("What symbol will player one use? (1 = O, 2 = X)");
    			
    			do{
    				
    				reselect = false;
    				playerChoice = input.nextInt();
    
    				if( playerChoice == 1 )
    				{
    					player1 = value.O;
    					player2 = value.X;
    				}
    				else if ( playerChoice == 2 )
    				{
    					player1 = value.X;
    					player2 = value.O;
    				}
    				else
    				{
    					System.out.println("Invalid selection, please select again");
    					reselect = true;
    				}
    			}
    
    			while( reselect == true );
    		}
    
    
    		catch (InputMismatchException inputMismatchException)
    		{
    			System.err.println("Please input an integer");
    			input.nextLine();
    		}
    
    	}
    there we go! oh wait... what happens after the exception is caught? the method would end, thats not good! well then clearly I must do this.

    Java Code:
    public void setPlayerSymbol()
    	{
    
    		try
    		{
    			int playerChoice;
    			boolean reselect;
    
    			System.out.println("What symbol will player one use? (1 = O, 2 = X)");
    
    			do{
    
    				reselect = false;
    				playerChoice = input.nextInt();
    
    				if( playerChoice == 1 )
    				{
    					player1 = value.O;
    					player2 = value.X;
    				}
    				else if ( playerChoice == 2 )
    				{
    					player1 = value.X;
    					player2 = value.O;
    				}
    				else
    				{
    					System.out.println("Invalid selection, please select again");
    					reselect = true;
    				}
    			}
    
    			catch (InputMismatchException inputMismatchException)
    			{
    				System.err.println("Please input an integer");
    				input.nextLine();
    			}
    
    			while( reselect == true );
    		}
    
    	}
    wait a minute, now my variable is out of scope... even if I initialise the reselect variable out of the try block, it still wont let me perform the while part after the catch clause.

    why this is logically impossible! (and I cant call a method inside the same method or it potentially leads to infinite method calls and stack overflow, so I need the do while part)

    how do you properly perform error handling on my simple method and make it robust?
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    7

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    Well, if you take a careful look at your last code snippet you'll see that your catch block is not in the right place. The catch block should be part of the try, and it obviously stands on its own now. Beside the try-catch, your do-while block also in error. The do-while statement should be closed with a correct while at the end.

    You might tried to place the try-catch block inside the do-while block. So your code should look like:

    Java Code:
    do {
        try {
            ....
        } catch (Exception e) {
            ....
        }
    } while (condition);
    EscSequenceAlpha likes this.

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    I was sure the logic wasnt there, no matter how I played with it something was always off... ok I've sorted it now

    Java Code:
    public void setPlayerSymbol()
    	{
    		boolean reselect;
    		int playerChoice;
    
    		System.out.println("What symbol will player one use? (1 = O, 2 = X)");
    
    		do
    		{
    			reselect = false;
    			
    			try
    			{
    				playerChoice = input.nextInt();
    
    				if( playerChoice == 1 )
    				{
    					player1 = value.O;
    					player2 = value.X;
    				}
    				else if ( playerChoice == 2 )
    				{
    					player1 = value.X;
    					player2 = value.O;
    				}
    				else
    				{
    					System.out.println("Invalid selection, please select again");
    					reselect = true;
    				}
    			}
    			catch (InputMismatchException inputMismatchException)
    			{
    				System.err.println("Please input an integer");
    				input.nextLine();
    			}
    		}
    		while( reselect == true );
    	}
    so this is the right structure for robust code? this combination of do while and exception handling, or do people use other ways
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    Note:
    Java Code:
    if(x == true)
    if(y == false)
    can be simplified to
    Java Code:
    if(x)
    if(!y)

  5. #5
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    ?

    if you're refering to
    Java Code:
    while( reselect == true );
    I already changed it to != that was just a typo not a logical error on my part
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    No I am not talking about changing == to !=. Read the above post again.

  7. #7
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    you think I'm using a redundant varriable, I guess I could use playerChoice as the do while condition

    but then I'd have to say playerChoice ( (!= 1) && (!= 2) ), I think it flows better with one condition when you're embedding code into a do while situation like this, maybe I'm wrong I dont know.
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    SIGH!

    No!

    I'm saying that comparing a boolean variable to true or false is redundant. The boolean variable already has a boolean value of true or false.

    Java Code:
    boolean flag = true;
    if(flag) {
        // will enter because flag is TRUE.
    }
    if(flag == true) {
        // will also enter but the comparison is redundant
    }

  9. #9
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    I see, I'm sorry the way you put it in your first post seemed like you were making the point that one variable could be used for two, but thats because I read your post as x == true and y == true therefore x and !x... did you edit your post? Im sure you made that typo

    doesnt matter thanks actually I didnt think of that.
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    Quote Originally Posted by EscSequenceAlpha View Post
    did you edit your post? Im sure you made that typo
    Then you would be wrong.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    Just wondering but why have the user enter 1 and 0 when they (and you) mean X andO?

    Moreover they also have to hit the enter key before you get any input. As you have it won't the newline be left in the input scanner's stream? This may not be a problem, but sometimes it can be.

    It seems to me that a Scanner is the wrong tool for the job. (The job being to read a line of input until it becomes equal to "X" or "O"). Consider a BufferedReader and handle faulty input as what it is: not exceptional.

  12. #12
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How do I apply exception handling (my error-proofing adventure inside)

    I guess the user could be prompted for X or O for input... thats more of a design choice isnt it, to be honest I didnt think of that but its a good idea.

    I'm not sure what you mean with the newLine(), I call newLine() because I know it took in an integer and I need it cleared so it can read the next input properly.

    I would absolutely consider a bufferedReader, I have no idea what it is but it sounds fantastic. I can however only work with the tools I have learned so far, I'm sure I'll end up replacing the scanner.

    sorry I misread your post then junkers
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

Similar Threads

  1. Exception handling
    By Moedig in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 10:43 PM
  2. exception handling
    By niksipandit in forum New To Java
    Replies: 3
    Last Post: 09-22-2011, 02:25 PM
  3. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  4. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:40 AM

Posting Permissions

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