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:
Code :
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
Re: Having trouble with an if statement. please help!!
Just return the appropriate string based upon the condition you already wrote:
Code :
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)
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! :confused:
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.
Code :
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];
}
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:
Code :
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!
Re: Having trouble with an if statement. please help!!
How about this!
Code :
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.
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.
:confused:
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.
Code :
if ((aNumber > 0) && (aNumber < 5))
{
... set diff. settings based on 1 to 4
..etc
}
else {
return "out of range";
}
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.
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???
Re: Having trouble with an if statement. please help!!
sorry my mistake, remove the else at the end, so the return is guaranteed.
ie.
Code :
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.
Re: Having trouble with an if statement. please help!!
Here you go - you don't need to IF crap
Code :
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.