return statement confusion
ok so i have a problem here...
i have 2 classes and one is calling another:
Code java:
public class DinnerMenu implements ItemListener
{
... //beginning of code
soup watery = new soup();
int dollar = 0;
public void itemStateChanged(ItemEvent e)
{
dollar += watery.getchangedollar();
... //rest of code
}
... //rest of code
}
and the second class is:
Code java:
public class soup
{
... // beginning of code
public int getchangedollar()
{
if (van.getStateChanged()==true)
return 2;
else if (veg.getStateChanged()==true)
return 3;
else if(broth.getStateChanged()==true)
return 4;
}
... //rest of code
}
i get an error: "missing return statement"
why?
also i tried to fix it by changing the second class:
Code java:
public class soup
{
... // beginning of code
public int getchangedollar()
{
int x;
if (van.getStateChanged()==true)
x=2;
else if (veg.getStateChanged()==true)
x=3;
else if(broth.getStateChanged()==true)
x=4;
return x;
}
... //rest of code
}
and there i get another error: "x might not have been initialized"
any help on why im getting these errors would be great, thanks
Dont be that guy
Re: return statement confusion
My guess is that it is upset as you haven't stated what to return in the event that all of your if and else if statements fail. Basically, I think it wants an else statement.
Re: return statement confusion
ok, the code is on another computer but ill give it a try... lets hope it works haha
thank you tho
Re: return statement confusion
sweet, it worked... i guess it requires a return statement for all possibilities, so having an else statement with noting in it gave me an error... had to put return 0; for it.
thanks
Re: return statement confusion
intialize the x value to 0 so that can remove the second error