Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: return statement confusion

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default return statement confusion

    ok so i have a problem here...

    i have 2 classes and one is calling another:

    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:

    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:

    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


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    that_guy (January 14th, 2011)

  4. #3
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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

  5. #4
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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

  6. #5

    Default Re: return statement confusion

    intialize the x value to 0 so that can remove the second error

Similar Threads

  1. Missing return statement
    By Stn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2011, 08:03 PM
  2. what is wrong with my return statement??????
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2010, 07:55 PM
  3. Missing Return Statement in If-elseIf-else
    By chronoz13 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 12th, 2009, 07:01 AM
  4. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM
  5. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM