954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with an java error

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");
}
}

fishguts
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

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
    }
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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!

fishguts
Newbie Poster
3 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You