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 12 of 12

Thread: Having trouble with an if statement. please help!!

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Having trouble with an if statement. please help!!

    Hi

    Im working on an if statement with the following specifications:

    If the methods argument is between 1 and 4 it should set the value of its
    "setting" instance variable to the value of the int argument. The method
    should then return a string showing the current setting – "setting1",
    "setting2", "setting3" or "setting4" depending on whether the "setting" instance variable
    has been set to 1, 2, 3 or 4. If the argument is not in the range it should return the string "out of range"

    the code I have so far is:

     public String whatSetting(int aNumber);
        {
             if ((aNumber > 0) && (aNumber < 5))
            {
                this.setting = aNumber;
                if (this.setting() == 1))

    I am now stuck on how to get it to return the different strings depending on the value of "setting" and return the "out of range" string if the argument is out of range.

    thanks in advance to anyone who can help


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Having trouble with an if statement. please help!!

    Just return the appropriate string based upon the condition you already wrote:
     
    public String whatSetting(int aNumber)
    {
             if ((aNumber > 0) && (aNumber < 5))
            {
                this.setting = aNumber;
               return "setting" + Integer.toString(aNumber);
             }else{
               return "out of range";
             }
    }
    (Technically you don't need the else, but I left it there for clarity)

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

    humdinger (January 23rd, 2010)

  4. #3
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with an if statement. please help!!

    hi sorry for the confusion but ive just looked at the specifications again & what I actually need to do is return "off" if the argument is 1, "low" if its 2, "medium" if its 3 and "high" if its 4. it must still return the string "out of range" if the argument is not in the range 1-4.

    sorry again for the confusion. hope someone can help me further!
    Last edited by humdinger; January 20th, 2010 at 12:05 PM.

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Having trouble with an if statement. please help!!

    You can chain if-else statements as long as you want, or you can use a switch statement. Another solution is to create an array of the possible selections based off of their indices, and return the correct string if it's in bounds that way.

    public String whatSetting(int aNumber)
    {
         String[] settings = {"off","low","medium","high"};
         aNumber--; // because array indices actually start at 0, let's update it right now
         if (aNumber < 0 || aNumber > 3)
         {
              return "out of range";
         }
         return settings[aNumber];
    }

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    humdinger (January 23rd, 2010)

  7. #5
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with an if statement. please help!!

    Hi helloworld922, thankyou very much for your help so far.

    I am unable to use arrays at this point. ive been working on this code some more but am still having problems! My new code is:

    public String whatSetting(int aNumber)
        {
            if ((aNumber < 1) || (aNumber > 4))
            {
                return "out of range";
            }
            else
            if ((aNumber > 0) && (aNumber < 5))
                 {
                     this.setting = aNumber;
                     if (this.setting == 1)
                     {
                         return "off";
                     }
                     if (this.setting == 2)
                     {
                         return "low";
                     }
                 }
            }

    I am getting the error "missing return statement" I know why this is but i just cant see a way to write the code to avoid the error!

  8. #6
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Having trouble with an if statement. please help!!

    How about this!


    public String whatSetting(int aNumber)
        {
            if ((aNumber > 0) && (aNumber < 5))
                 {
                     this.setting = aNumber;
                     if (this.setting == 1)
                     {
                         return "off";
                     }
                     if (this.setting == 2)
                     {
                         return "low";
                     }
                     If (this.setting == 3)
                     {
                          return "medium";
                     }
                     if (this.setting == 4) 
                     {
                          return "high";
                     }
                 }
                 else {
                    return "out of range";
                 }
            }

    Then a return is always executed.

  9. The Following User Says Thank You to JavaDaveUK For This Useful Post:

    humdinger (January 23rd, 2010)

  10. #7
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with an if statement. please help!!

    Hi JavaDaveUK,

    Thankyou for your help.

    I had already tried that earlier and it still said "missing return statement". I just tried re-writing it again as to your post and it still returns the same error.


  11. #8
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Having trouble with an if statement. please help!!

    are you sure you have the else from the first if, not the if that results in the high setting.

    i.e.
    if ((aNumber > 0) && (aNumber < 5))
    {
                 ... set diff. settings based on 1 to 4
                 ..etc
    }
    else {
        return "out of range";
    }

  12. The Following User Says Thank You to JavaDaveUK For This Useful Post:

    humdinger (January 23rd, 2010)

  13. #9
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with an if statement. please help!!

    Hi just to let you know i work nights so im at work now. il be back working on this tomorrow so il look into it then. thankyou very much for all your help so, I will be sure to click the thankyou user when the thread is finished.

  14. #10
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with an if statement. please help!!

    Hi, I have checked the code and i have it down exactcly as you said but it still returns the same error. any ideas???

  15. #11
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Having trouble with an if statement. please help!!

    sorry my mistake, remove the else at the end, so the return is guaranteed.

    ie.
    if ((aNumber > 0) && (aNumber < 5))
    {
                 ... set diff. settings based on 1 to 4
                 ..etc
    }
    return "out of range";

    As stated on another post arrays would be the best way of doing this and having the out of range as the first if and then returning the correct element in the array.
    But in your case, this will suffice.

  16. The Following User Says Thank You to JavaDaveUK For This Useful Post:

    humdinger (January 23rd, 2010)

  17. #12
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Having trouble with an if statement. please help!!

    Here you go - you don't need to IF crap

    	public String whatSetting(int aNumber)
    	{
    		this.setting = aNumber;
    		switch(aNumber)
    		{
    		case 1: return "off";
    		case 2: return "low";
    		case 3: return "medium";
    		case 4: return "high";
    		default: return "out of range";
    		}
    	}

    This will handle ANY int you pass. Also, if the settings change to include 0,5 or anything else it is easy to maintain.

  18. The Following User Says Thank You to mdv2000 For This Useful Post:

    humdinger (January 23rd, 2010)

Similar Threads

  1. Grade Array Trouble
    By kite98765 in forum Collections and Generics
    Replies: 3
    Last Post: January 7th, 2010, 08:14 PM
  2. Simple server-client trouble
    By DC200 in forum Java Networking
    Replies: 3
    Last Post: November 12th, 2009, 08:16 AM
  3. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM
  4. Having trouble redirecting nodes
    By KingLane in forum Collections and Generics
    Replies: 6
    Last Post: October 19th, 2009, 06:46 PM
  5. Trouble in downloading java
    By captjade in forum Java Theory & Questions
    Replies: 6
    Last Post: March 3rd, 2009, 04:16 PM