Reputation: 15
I've been tasked with creating a small java program for a course i'm doing, and i'm having some issues with the following block of code:
System.out.println("Please enter your first name:");
userName = sc.nextLine();
System.out.println("What year were you born in?");
birthYear = sc.nextInt();
System.out.println("In the 'dd.mm' format, what are the day and month of your birth?");
dayMonth = sc.nextDouble();
//Cast the user input of type double to an int for the day of Birth
dayOfBirth = dayMonth.intValue();
//Cast the double value for month and cast it to an int
dayMonth = dayMonth - dayOfBirth;
dayMonth = dayMonth * 100;
dayMonth = Math.rint(dayMonth);
monthOfBirth = dayMonth.intValue();
if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941)
{
if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
{
System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
}
else if(monthOfBirth != 9 || monthOfBirth != 10 || monthOfBirth != 11)
{
System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
}
}
if(birthYear != 2013 || birthYear != 2001 || birthYear != 1989 || birthYear != 1977 || birthYear != 1965 || birthYear != 1953 || birthYear != 1941)
{
if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
{
System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
}
else if(monthOfBirth != 9 || monthOfBirth != 10 || monthOfBirth != 11)
{
System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
}
}
However my output is coming out as follows:
Please enter your first name:
Samuel
What year were you born in?
1977
In the 'dd.mm' format, what are the day and month of your birth?
09.09
Samuel was born during Spring on September 9 in Snake year 1977
Samuel was born during Spring on September 9
I obviously do not want repeated output, however i'm having trouble seeing where the logic in my if statements are causing it to be repeated.
Any help would be greatly appreciated.
Thanks all,
~ Sam.
Upvotes: 1
Views: 226
Reputation: 5522
The problem is with
if(birthYear != 2013 || birthYear != 2001 || birthYear != 1989 || birthYear != 1977 || birthYear != 1965 || birthYear != 1953 || birthYear != 1941
If you look carefully, we are making sure that the expression always result in try. Only one can be false at max at a time, all others will be true, hence finally it is always true (confused?). Say birth year is 2013, which makes first condition (birthyear!=2013) false, but all others will be true. Take any other example and you will see what I am trying to say.
Actually a minor modification will fix the code for you. You have already checked that if (birthyear is in given years)
, a simple else without any conditions will work for you.
if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941)
{
if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
{
System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
}
else// NO CHECK REQUIRED NOW
{
System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear);
}
}
//SEE THIS
else{
if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11)
{
System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
}
//AND
else
{
System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear);
}
}
Upvotes: 0
Reputation: 1585
Better use collections instead of multiple if( ... == ..|| ... == .. ||...)
Like this:
List<String> wrongYears = Arrays.asList("2013", "2001", "1989", "1977", ...);
and check:
if(wrongYears.contains(birthYear)
{
...
}
and for months numbers the same
your logic errors would be obvious
Upvotes: 1
Reputation: 10666
The answer is in your conditional.
assume dob = 1
if(dob == 1 || dob == 2 || dob == 3 ...)
Will evaluate to true because dob == 1
if (dob != 1 || dob != 2 || dob != 3 ...)
Will also evaluate to true because dob != 2
What you want to use is AND
/ &&
and not OR
/ ||
Upvotes: 0
Reputation: 663
you can choose one of the following solutions:
1) using && operator instead of || in 2nd if statement
or
2) if(){}else{} // Kamal's Solution
Upvotes: 1
Reputation: 313
The second if part is wrong:
if(birthYear != 2013 || birthYear != 2001 || ....)
because it is alway true. Just check a view examples: If you have 2012, It is not 2013 so your expression becomes true. If you take 2013 then the first part becomes false, but when you evaluate 2013 != 2001, it becomes true again.
What you probably want to have is
if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941)
{
.....
}
else {
.....
}
By the way: If you want to negate the expression (x == A || x == B || x == C) then the logical negation is (x!=A && x!=B && x!=C).
Upvotes: 0
Reputation: 1987
You should replace the second if where everything is != with a simple 'else' because you have already checked if any of the statements are true. As such, simply writing else will be logically equal and much more readable.
if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941){
....
}
else { //not snake year
....
}
Upvotes: 0
Reputation: 1669
Use else statement where possible, so if you test for 2013, 2001 ... Just after, make an else statement
Upvotes: 0
Reputation: 45060
Instead of using 2 if
separately, make them either if-else
or if-else if
.
if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941){
....
}
else if(birthYear != 2013 || birthYear != 2001 ||...){
....
}
Because, if you give a if-if
, both the if
statements will be executed, whereas, if you give an if-else if
, the second else if
will be executed only if the first if
statement fails.
Upvotes: 1