Spectator Logic
Spectator Logic

Reputation: 15

Missing return statement : try-catch

There is a return statement inside the try and the catch block but there still occures an error: Missing return statement. i'm using BlueJ btw.

public int[] getInput(){
    boolean rightInput = false;
    int[] nothing = {0,0};
    while(rightInput == false){
      try{
       rightInput = true;
      int[] XY = {0,0};
      String coordinates;
      System.out.println("Insert coordinates (x y) and press enter.");
      coordinates = input.nextLine();
      XY[1] = Character.getNumericValue(coordinates.charAt(0));
      XY[0] = Character.getNumericValue(coordinates.charAt(2));
      return XY;
     }catch(StringIndexOutOfBoundsException se){
      System.out.println(se);
      System.out.println("Try Again with a space between x and y.");
      rightInput =  false;  

      return nothing;
     }

    }

}

Upvotes: 1

Views: 1929

Answers (4)

Kushagra Misra
Kushagra Misra

Reputation: 481

what i can understand from your code you want to perform an operation in a while loop till rightInput becomes true and if any error occurs in between, it should not break the loop.

what you are doing is return value after each time loop executes. where return itself will take you out of the loop , for that we use break operator.

I would suggest you to move your return XY out of the loop. Please find update code below :

    public int[] getInput(){
       boolean rightInput = false;
       //int[] nothing = {0,0}; not required any more
       int[] XY = {0,0}; // moved outside loop
       while(rightInput == false){
       try{
            rightInput = true;
            String coordinates;
            System.out.println("Insert coordinates (x y) and press enter.");
            coordinates = input.nextLine();
            XY[1] = Character.getNumericValue(coordinates.charAt(0));
            XY[0] = Character.getNumericValue(coordinates.charAt(2));
       } catch(StringIndexOutOfBoundsException se) {
            System.out.println(se);
            System.out.println("Try Again with a space between x and y.");
            rightInput =  false;

           // return nothing; not required any more it is same as XY
        }

    }
    return XY;
    }

Upvotes: 0

Abdo Bmz
Abdo Bmz

Reputation: 641

Change your code to this :

 public class JavaAPP {

         public int[] getInput(){
        boolean rightInput = false;
        int[] nothing = {0,0};
        while(rightInput == false){
          try{
           rightInput = true;
          int[] XY = {0,0};
          String coordinates;
          System.out.println("Insert coordinates (x y) and press enter.");
          coordinates = input.nextLine();
          XY[1] = Character.getNumericValue(coordinates.charAt(0));
          XY[0] = Character.getNumericValue(coordinates.charAt(2));
          return XY;
         }catch(StringIndexOutOfBoundsException se){
          System.out.println(se);
          System.out.println("Try Again with a space between x and y.");
          rightInput =  false;  

          return nothing;
         }

        }
             return null;

    }  
        public static void main(String[] args) {

    JavaAPP Obj = new JavaAPP (); 
    Obj.getInput(); 

    }

    }

Upvotes: 0

Thiyagu
Thiyagu

Reputation: 17900

There has to be a return statement after the while loop as possibly control does not go into the while loop body at all (when rightInput is true)1 and the method has to return something in that case.

Moving return nothing; after the while block will work for you.

Or, you can make the while loop condition as true

while (true) {
  try {
    ...
    return XY;

  } catch(...) {
    ...
    return nothing;
  }

}

1 Though in your case rightInput is always false, the compiler does not infer that (maybe cannot?) and it is a good thing. Think of what would happen if there is a specialized logic to compute rightInput dynamically. Then it would result in a compilation error mandating to add a return statement as the compiler can now in no way tell whether the while loop body would be executed or not.

Upvotes: 1

Imal
Imal

Reputation: 581

What if your code does not meet the condition specified in the while loop so does not go inside the while loop? So to cover that scenario there should be a return statement outside the while loop too. Just put

return null

just before the end of the method.

while{
  try {

  }
  catch {

  }
}
return null;

Upvotes: 0

Related Questions