Results 1 to 6 of 6
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Can't figure out why this error is happening? Try catch statement error

    I'm using this try statement to check for errors in input in my Tic Tac Toe game.
    The problem I have having is I don't understand why this error is happening. It's preventing my code from compiling.
    I've tried inserting an else statement and multiple brackets at various places with no success.
    Could someone please clarify this for me?
    Java Code:
      
    public void play(){
    	    Scanner input = new Scanner(System.in);
    	    int row, col;
    	    char currPlayer = 'X';
    	    while(!isFull() && winner() != 'X' && winner() != 'O'){
    		      try{
    		        print();
    		        System.out.println("Player "+currPlayer+"'s Turn");
    		        System.out.print("Enter your move. (e.g. 1,2):");
    		        Scanner tokens = new Scanner(input.nextLine());
    		        tokens.useDelimiter(",");
    		        if (input.hasNextInt()){
    		        	row = tokens.nextInt();
    		        if (input.hasNextInt()){
    		        	col = tokens.nextInt();
    		        if (!set(row,col,currPlayer)){
    		               throw new Exception("Error Setting.");
    		        	}catch(Exception error){ //error happens here. 
                            //error with }, saying I have to insert else statement to complete if statement, 
                            //or } to complete Block
    		              if(error instanceof InputMismatchException){
    		                input.nextLine();
    		              }else{
    		          System.out.println("Bad input");
    	}
    	        if(currPlayer == 'X'){
    	          currPlayer = 'O';
    	        }
    	        else{
    	          currPlayer = 'X';
    	        }
    	      }
    	      
    	      if(currPlayer == 'X'){
    	        currPlayer = 'O';
    	      }
    	      else{
    	        currPlayer = 'X';
    	      }
    	    }
    	 
    	    input.close();
    	    print();
    	        
    	        }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Can't figure out why this error is happening? Try catch statement error

    That close brace before the "catch" keyword is colsing the if statement block before it, NOT the try block. You seem to have forgotten to close the if statement block.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: Can't figure out why this error is happening? Try catch statement error

    You get that error because your code syntax is incorrect. See those three if statements that you have lined up one over the other above the line that causes the compilation error? Aren't they missing something?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Can't figure out why this error is happening? Try catch statement error

    If I try doing that though, then it gives me the error that the variables row and col "may not have been initialized."

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Can't figure out why this error is happening? Try catch statement error

    Because that is true. You have declared those variables, but you only define them inside of the if statements. The compiler CANNOT be certain that those variables will have values before attempting to USE those values (i.e. in the condition of the third if statement).

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: Can't figure out why this error is happening? Try catch statement error

    Quote Originally Posted by Propinquity View Post
    If I try doing that though, then it gives me the error that the variables row and col "may not have been initialized."
    Errors are your friend you know; you don't want them to go away, you want to -solve- them. That requires reading and understanding them first. So give your good friend who is trying to help you the attention it deserves!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Cannot figure out reason for error..
    By xKxCx in forum New To Java
    Replies: 5
    Last Post: 07-21-2013, 11:54 AM
  2. catch error statement
    By droidus in forum New To Java
    Replies: 11
    Last Post: 03-16-2012, 10:49 AM
  3. try, catch, exception error
    By Asvin in forum New To Java
    Replies: 5
    Last Post: 04-11-2011, 11:12 PM
  4. Can someone figure out this error for me
    By Dev23 in forum New To Java
    Replies: 2
    Last Post: 02-24-2011, 05:31 AM
  5. Error with my Try Catch
    By kewlgeye in forum New To Java
    Replies: 9
    Last Post: 05-03-2008, 04:38 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
  •