I am working on a java assignment for my cs 111 class and I keep getting an missing return statement error, and have tried to determine the error but cannot locate it. I am sure that I have the right set of curly braces. My code is shown below. If anyone can offer advice to correct the error I would greatly appreciate it.

import java.util.*;
import java.io.*; 

//Declare a class
public class Assingment3{


    //make the doMath method
    static int doMath(char op,int numa, int numb){

        switch (op)

        {
        case '+': 
        return numa + numb;

        case '-':
        return numa - numb;

        case '*':
        return numa * numb;

        case '/':
        return numa / numb;
        }

    }

    //Make the exponent method
    static int exponent(int a, int b){
        int return_val = 1; 

        if (b < 0) 
        {
        System.out.println("Error: Exponent Must Be >=0");
        return_val = 1;
        }

        else if (b == 0) 
        {
        return_val = 1;
        }

        else if (b == 1) 
        {
        return_val = a;
        }

        else 
        {
        return_val = a * exponent (a, b-1);
        }
        return (return_val);
        }




//Declare main      
public static void main(String[] args){

    //Set up Scanner to take input from the user
    Scanner sc = new Scanner(System.in);

    //Declare the variables
    int inta, intb, result0;
    String op;  
    char charop;

    //Ask the user to input an integer expression
    System.out.println ("Enter a simple integer expression:");  

    //Assing values entered by the user
    inta = sc.nextInt();
    op = sc.next();
    charop = op.charAt(0);
    intb = sc.nextInt();


    //Present the output to the user
    System.out.print("This Program is Not Complete Yet");
    }
}

Recommended Answers

All 3 Replies

You have no guaranteed return statement in doMath(). If op falls through all of the cases in your switch, the method would not return a value. Add a default return value after the switch and it will be fine

static int doMath(char op,int numa, int numb){
        
        switch (op)
        
        {
            case '+':
                return numa + numb;
                
            case '-':
                return numa - numb;
                
            case '*':
                return numa * numb;
                
            case '/':
                return numa / numb;
        }
        return 0;  // or another default if you prefer
    }

Or better yet, write it to have a single exit point...

static int doMath(char op,int numa, int numb) {
        int result;
        switch (op) {
            case '+':
                result = numa + numb;
                break;
            case '-':
                result = numa - numb;
                break;
            case '*':
                result = numa * numb;
                break;
            case '/':
                result = numa / numb;
                break;
            default:
                result = 0;
        }
        return result; 
   }

though I'd probably not return 0 on an invalid operator but throw an IllegalArgumentException.
Returning 0 WILL lead to hard to find bugs later, as 0 is a valid return value for any of the valid operations as well.

Or better yet, write it to have a single exit point...

static int doMath(char op,int numa, int numb) {
        int result;
        switch (op) {
            case '+':
                result = numa + numb;
                break;
            case '-':
                result = numa - numb;
                break;
            case '*':
                result = numa * numb;
                break;
            case '/':
                result = numa / numb;
                break;
            default:
                result = 0;
        }
        return result; 
   }

though I'd probably not return 0 on an invalid operator but throw an IllegalArgumentException.
Returning 0 WILL lead to hard to find bugs later, as 0 is a valid return value for any of the valid operations as well.

Thanks for the help. I got everything working how it should be!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.