| | |
Help with an java error
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Solved Threads: 0
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");
}
}
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");
}
}
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
Java Syntax (Toggle Plain Text)
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...
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.
Java Syntax (Toggle Plain Text)
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Sep 2007
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
Or better yet, write it to have a single exit point...
Java Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Java Error (Java)
- Java Causes IE to Crash...Java not working (Web Browsers)
- I need a good java compiler (Java)
- Error message when trying to install Java (Windows 95 / 98 / Me)
- DNS: RouteThrough , " RT" in java networking programming (Java)
- Help Java Error "Java not found" (HTML and CSS)
- programme runing error (Windows NT / 2000 / XP)
- DNS Error (Web Browsers)
- java chat problem (Windows 95 / 98 / Me)
Other Threads in the Java Forum
- Previous Thread: cancelling the actionPerformed
- Next Thread: Jdbc
Views: 811 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database detection draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle os output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong






