Shinobi1173
Shinobi1173

Reputation: 97

Java error missing return statment

Alright, I am trying to write this code but I keep getting this stupid error. I have no idea what I am doing wrong so maybe one of you experts can help me out.

import java.util.*;

public class School
{
Random randQuest = new Random();

int userAnswer;

public void createQuestion()
{

    int range = 10; // range of numbers in question

    int num1 = randQuest.nextInt( range );
    int num2 = randQuest.nextInt( range );

    userAnswer = num1 * num2;

    System.out.printf( "How much is %d times %d?\n",
        num1, num2 );

}

// prompt comment
public String promComm( boolean answer )
{
    if ( answer )
    {
        switch ( randQuest.nextInt( 1 ) )
        {

        case 0:
            return( "Very Good!" );

        }

        switch ( randQuest.nextInt( 1 ) )
        {

        case 0:                 
            return( "No. Please try again." );

        }
    }
}

}

Upvotes: 0

Views: 137

Answers (6)

Morteza Adi
Morteza Adi

Reputation: 2473

when a method has a return type all its code flows must return a value. in your code if the answer is false promComm() doesn't run into if block and never return value!!

Beside its not recommended to use multiple return in a method!

for example you can avoid this by:

public String promComm( boolean answer ) {
    String returnValue = "Answer is false"; //or = ""
if ( answer )
{
    switch ( randQuest.nextInt( 1 ) )
    {

    case 0:
        returnValue = "Very Good!";

    }

    switch ( randQuest.nextInt( 1 ) )
    {

    case 0:                 
         returnValue = "No. Please try again.";

    }
 }
return returnVal;
}

Upvotes: 1

Jayant Varshney
Jayant Varshney

Reputation: 1825

try writing this

 public String promComm( boolean answer )
    {
        if ( answer )
        {
            switch ( randQuest.nextInt( 1 ) )
            {
               case 0:  return( "Very Good!" );
            }
            switch ( randQuest.nextInt( 1 ) )
            {
                case 0: return( "No. Please try again." );
            }
        }
        return "";
    }

Upvotes: 1

chuntato
chuntato

Reputation: 128

return a String result after the if-else statement

public String promComm( boolean answer ){

   if(answer){
       ...

   }else{

      ...
   }

return ""; //empty string <-- you are missing a default return statement
}

Upvotes: 1

Grisha Weintraub
Grisha Weintraub

Reputation: 7986

In method promComm if answer is false the method does not return any value. Same if randQuest.nextInt(1) != 0.

Should be something like :

public String promComm( boolean answer ){
    if (answer){

        switch (randQuest.nextInt(1)){    
           case 0:
               return("Very Good!");
        }

        switch (randQuest.nextInt(1) ){
           case 0:                 
               return( "No. Please try again." );
        }

        return "Some value";

    }else

        return "Some value";
  }

Upvotes: 1

Bharat Sinha
Bharat Sinha

Reputation: 14363

Your method promComm should return a String in every case which it doesn't. The simplest way to fix it to add a default return statement.

public String promComm( boolean answer ){
    if ( answer ){
        //...
    }
    return "default value when answer is false.";
}

Upvotes: 1

Desislav Kamenov
Desislav Kamenov

Reputation: 1203

promComm method is returning String but if the switching value is not 0 then your function will return nothing - there is no default return statement.

Upvotes: 1

Related Questions