I tried running a program i wrote is a program tha calculates two numbers . And is telling me a missing return statement. what do i do
Missing Return Statement
Page 1 of 19 Replies - 863 Views - Last Post: 13 July 2010 - 07:18 AM
Replies To: Missing Return Statement
#2
Re: Missing Return Statement
Posted 13 July 2010 - 06:36 AM
Zinny, on 13 July 2010 - 06:05 PM, said:
I tried running a program i wrote is a program tha calculates two numbers . And is telling me a missing return statement. what do i do
did you forget to add return statement to method...
Please post code to see what you have done!!
This post has been edited by Krishs: 13 July 2010 - 06:38 AM
#3
Re: Missing Return Statement
Posted 13 July 2010 - 06:51 AM
Keep in mind that if your method type is not void you have to be returning something.
For example, if your method is "public double getDouble()" you need to have something along the lines of "return doubleVariable".
Also, another reason this could be happening is if you have a method that looks something like this:
The above example will return an error because a double will only be returned if x > 10.
Hope this helps, post your code and I can assist you further.
For example, if your method is "public double getDouble()" you need to have something along the lines of "return doubleVariable".
Also, another reason this could be happening is if you have a method that looks something like this:
public double getDouble()
{
if(x > 10)
{
double doubleVariable = 2.0;
return doubleVariable;
}
}
The above example will return an error because a double will only be returned if x > 10.
Hope this helps, post your code and I can assist you further.
This post has been edited by eZACKe: 13 July 2010 - 06:53 AM
#4
Re: Missing Return Statement
Posted 13 July 2010 - 06:54 AM
Title renamed to be more descriptive.
We can't help you if you don't post your code as well as where you are getting your errors.
We can't help you if you don't post your code as well as where you are getting your errors.
#5
Re: Missing Return Statement
Posted 13 July 2010 - 06:54 AM
class Calculation
{
int x;
int y;
int z;
public Calculation(int x, int y)
{
this.x=x;
this.y=y;
}
public int add ( )
{
z=x+y;
System.out.println("x+y == " + x +" "+ y + " ==" + z);
}
public static void main(String[ ] args)
{
Calculation c = new Calculation (20 ,10);
c .add( );
}
}
Edited by Dogstopper, please
This post has been edited by Dogstopper: 13 July 2010 - 07:13 AM
#6
Re: Missing Return Statement
Posted 13 July 2010 - 06:57 AM
It's just as I said above, your method is type int but you are not returning anything. If you just want to print and not return an actual value, use type void.
So you want:
So you want:
public void add()
#7
Re: Missing Return Statement
Posted 13 July 2010 - 07:06 AM
is still not finding my call
#8
Re: Missing Return Statement
Posted 13 July 2010 - 07:11 AM
Zinny, on 13 July 2010 - 06:06 AM, said:
is still not finding my call
As they said above, you have to use void if function doesn't return anything.
class Calculation
{
int x;
int y;
int z;
public Calculation(int x, int y)
{
this.x=x;
this.y=y;
}
public void add ( ) // LOOK HERE
{
z=x+y;
System.out.println("x+y == " + x +" "+ y + " ==" + z);
}
public static void main(String[ ] args)
{
Calculation c = new Calculation (20 ,10);
c .add( );
}
}
Edited by Dogstopper, we'd prefer that you have code tags and a comment than without code tags. Thank you.
This post has been edited by Dogstopper: 13 July 2010 - 07:15 AM
#9
Re: Missing Return Statement
Posted 13 July 2010 - 07:13 AM
Once you make the method void, it does work.
I just ran it here's the result:
"x+y == 20 10 ==30"
I just ran it here's the result:
"x+y == 20 10 ==30"
#10
Re: Missing Return Statement
Posted 13 July 2010 - 07:18 AM
If you'd rather that the add method add and NOT print, then you can do something more like this, using the return statement.
Often, this method is liked better since now you can use add() elsewhere since it returns a value. (You can do more things using value than just printing)
class Calculation
{
int x;
int y;
int z;
public Calculation(int x, int y)
{
this.x=x;
this.y=y;
}
public int add ( ) // specify it returns an int
{
z=x+y; // Add the numbers
return z; // This "Gives" z back to the method that called add().
}
public static void main(String[ ] args)
{
Calculation c = new Calculation (20 ,10);
System.out.println(c .add( )); // This calls add() and prints the result
}
}
Often, this method is liked better since now you can use add() elsewhere since it returns a value. (You can do more things using value than just printing)
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|