Help with an java error

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 2
Reputation: fishguts is an unknown quantity at this point 
Solved Threads: 0
fishguts fishguts is offline Offline
Newbie Poster

Help with an java error

 
0
  #1
Sep 28th, 2007
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");
}
}
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with an java error

 
0
  #2
Sep 28th, 2007
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
  1. static int doMath(char op,int numa, int numb){
  2.  
  3. switch (op)
  4.  
  5. {
  6. case '+':
  7. return numa + numb;
  8.  
  9. case '-':
  10. return numa - numb;
  11.  
  12. case '*':
  13. return numa * numb;
  14.  
  15. case '/':
  16. return numa / numb;
  17. }
  18. return 0; // or another default if you prefer
  19. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Help with an java error

 
0
  #3
Sep 29th, 2007
Or better yet, write it to have a single exit point...
  1. static int doMath(char op,int numa, int numb) {
  2. int result;
  3. switch (op) {
  4. case '+':
  5. result = numa + numb;
  6. break;
  7. case '-':
  8. result = numa - numb;
  9. break;
  10. case '*':
  11. result = numa * numb;
  12. break;
  13. case '/':
  14. result = numa / numb;
  15. break;
  16. default:
  17. result = 0;
  18. }
  19. return result;
  20. }

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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: fishguts is an unknown quantity at this point 
Solved Threads: 0
fishguts fishguts is offline Offline
Newbie Poster

Re: Help with an java error

 
0
  #4
Sep 29th, 2007
Originally Posted by jwenting View Post
Or better yet, write it to have a single exit point...
  1. static int doMath(char op,int numa, int numb) {
  2. int result;
  3. switch (op) {
  4. case '+':
  5. result = numa + numb;
  6. break;
  7. case '-':
  8. result = numa - numb;
  9. break;
  10. case '*':
  11. result = numa * numb;
  12. break;
  13. case '/':
  14. result = numa / numb;
  15. break;
  16. default:
  17. result = 0;
  18. }
  19. return result;
  20. }

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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 811 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC