Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile error on accessor method

Status
Not open for further replies.

tberger1

MIS
Sep 12, 2006
16
US
I am trying to write a get method:

public void getGasConsumed()
{
int gals = miles/fuelEffiency;
return gals;
}

when I try to compile it I get the following error:

cannot return a value from method whose result type is void
return gals;

I am not understanding the problem - any help would really be appreciated.
 
I tried that - now I get this compile error:

C:\Documents and Settings\NextStep\Desktop\OdometerA.java:13: missing return statement
}
^
C:\Documents and Settings\NextStep\Desktop\OdometerA.java:18: missing return statement
}
^
C:\Documents and Settings\NextStep\Desktop\OdometerA.java:23: missing return statement
}
^

Here is the full code:

import java.util.Scanner;

public class OdometerA

{
private int miles;
private int fuelEffiency;

public int setMiles()
{
miles = 0;
}

public int setFuelEffiency(int newFuelEffiency)
{
fuelEffiency = newFuelEffiency;
}

public int setLogMiles(int numMilesDriven, int countMiles)
{
countMiles = miles + numMilesDriven;
}

public int getGasConsumed()
{
int gals = miles/fuelEffiency;
return gals;
}

}

Is there something else I am doing wrong??
 
public int setMiles()
{
miles = 0;
}

public int setFuelEffiency(int newFuelEffiency)
{
fuelEffiency = newFuelEffiency;
}

public int setLogMiles(int numMilesDriven, int countMiles)
{
countMiles = miles + numMilesDriven;
}

In all three of these methods you declare a return int but the method doesn't actually return anything.

_________________
Bob Rashkin
 
Thanks for that - I changed them to public void and it is now compiling.

Appreciate the help - hopefully I can get through the rest on my own.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top