My if-else statment wont return correct value...
I am fairly new to Java so please don't flame me if I did something simple and stupid. But my problem is that I have a basic if-elseif statement that looks at a certain value and if it higher than 12 or lower than 1 then it should return the number as 1. Now, this isn't my whole program but this seems to be the only part I am having trouble with.
publicclass VerheyenCh6Ex5
{
publicstaticvoid main(String args[])
{
Month m1 =new Month();
// Display details of m1
System.out.println(m1);
}
}
class Month
{
privateint monthNumber;
private String monthName;
public Month()
{
monthNumber = 13;
}
publicvoid setMonthNumber(int mNum)
{
monthNumber = mNum;
if (mNum < 1)
mNum = 1;
elseif (mNum > 12)
mNum = 1;
}
publicint getMonthNumber()
{
return monthNumber;
}
}
Whenever I enter in a number, say 13, it shows up "null". Any help is greatly appreciated!!
You're not calling your set or get methods, so they might as well not be there.And no, that code does NOT print out null. It prints out something like Month@121fd61, because you didn't override toString.It's really not clear what problem you're having.
public void setMonthNumber(int mNum)
{
monthNumber = mNum;
if (mNum < 1)
mNum = 1;
else if (mNum > 12)
mNum = 1;
You want to set the monthNumber to 1 if the given value is invalid right? You can check whether it's less than 1 or greater than 12 in one check,
if (mNum <1 || mNum > 12) {// invalid
monthNumber = 1;
}
else {
monthNumber = mNum;
}
This way, if it's invalid, the monthNumber, not the mNum gets set to 1, otherwise, it's set to mNum. Get it?
I have called them, I just didn't include them in my code....I am just trying to figure out the if statement.
Ooh okay, i gotcha....ill try that out!
Interesting...the results still show up null....would it help if i posted my whole program?
> Interesting...the results still show up null....would
> it help if i posted my whole program?
Well, if you want help debugging some code, it would be useful if you posted the actual code that you're running.
I don't know that you need to post the entire thing, but enough of it that we can see the relevant pieces. Or better yet, extract the relevant pieces into a small, self-contained program that exhibits the same behavior and paste that.
My crystal ball tells me though that you're expecting the month name to show up but it never does because either you've never set it or you had a local variable hiding the member variable when you set it.
Here is the full code....the program takes the monthNumber and converts it to the name of the month via the getMonthName() method.
public class VerheyenCh6Ex5
{
public static void main(String args[])
{
Month m1 = new Month();
// Display details of m1
System.out.println(m1);
}
}
class Month
{
private int monthNumber;
private String monthName;
public Month()
{
monthNumber = 31;
}
public void setMonthNumber(int mNum)
{
monthNumber = mNum;
if (mNum <1 || mNum > 12)
monthNumber = 1;
else
monthNumber = mNum;
}
public int getMonthNumber()
{
return monthNumber;
}
public String getMonthName()
{
if (monthNumber == 1)
monthName = "January";
else if (monthNumber == 2)
monthName = "February";
else if (monthNumber == 3)
monthName = "March";
else if (monthNumber == 4)
monthName = "April";
else if (monthNumber == 5)
monthName = "May";
else if (monthNumber == 6)
monthName = "June";
else if (monthNumber == 7)
monthName = "July";
else if (monthNumber == 8)
monthName = "August";
else if (monthNumber == 9)
monthName = "September";
else if (monthNumber == 10)
monthName = "October";
else if (monthNumber == 11)
monthName = "November";
else if (monthNumber == 12)
monthName = "December";
return monthName;
}
public String toString()
{
return getMonthName();
}
public boolean equals(Month m)
{
boolean status;
if (m.getMonthNumber() == monthNumber)
status = true;
else
status = false;
return status;
}
public boolean greaterThan(Month m)
{
boolean status;
if (m.getMonthNumber() > monthNumber)
status = true;
else
status = false;
return status;
}
public boolean lessThan(Month m)
{
boolean status;
if (m.getMonthNumber() == monthNumber)
status = true;
else
status = false;
return status;
}
}
Okay, and what sequence of statements do you think is being executed that will set monthName?Step through your code with pencil and paper, or a debugger, of a bunch of print statements.
The program works and displays all the months jan - dec except when i enter in an invalid number, that is when the number should go back to 1 and display january.
> The program works and displays all the months jan -
> dec except when i enter in an invalid number, that is
> when the number should go back to 1 and display
> january.
See reply 8.
Your program is not doing what you think. It's your job to examine it in action to see what it is actually doing. It's quite a simple oversight. Tracking it down yourself will be good practice.
> The program works and displays all the months jan -
> dec except when i enter in an invalid number, that is
> when the number should go back to 1 and display
> january.
Do you mean it doesn't work when you put 31 in the constructor?
public Month()
{
monthNumber = 31;
}
Your setMonthNumber method is what validates that value, if you set it directly you're bypassing the checks and storing a bad value. You could use the setter in the constructor to take advantage of that code.
I am still kinda confused...please remember, im VERY new to java and sometimes it takes someone to blatantly point something out to me for me to understand..... :(
> I am still kinda confused...please remember, im VERY
> new to java and sometimes it takes someone to
> blatantly point something out to me for me to
> understand..... :(
If you put in 31, what sequence of steps do you think will occur that will give you "January" for the month name? It's your code. If you wrote it, you should be able to predict what will happen in all situations.
So, what are the steps? How do we get from 31 to "January"?