You are attempting to compare Strings using '==' which only compares the references. You need to be using the 'equals()' method instead which compares the characters.
When you have a chain of if/else statements you need one extra else at the end to catch any uncaught value and print out an error message showing the invalid value and saying what is wrong. That way there will always be some notice taken of invalid input to assist in problem solving.
A suggestion for methods named get.... they should return a value that the calling method can save and use.
What have you been taught about else‑ifs? Nobody indents them like that in real life. What people do is miss out the { before if, and its partner. There are also orders you can put the ifs in, but that doesn't matter in your case. You end up with code like this:-Try that, which should make your code easier to read.
Carey has told you the real problem, using the same object operator == I suggest you try a switch‑case, which can take Strings for the cases.
Adding to the previous responses from the colleagues, I agree with what Campbell Ritchie said and I encourage you to use switch-case for the getMonth() method, which will improve the readability and performance of your code.
I would prefer to see objects introduced early in your training. I know some people are quite happy with late objects, but you have shown us some non‑object‑oriented code.
I would also prefer to see all those fields and the getSeason() method given private access. There is no need for them to be accessed outwith the current class. I think you can delete month1 because it isn't used at all.
I also tried a switch‑case. Look in Cay Horstmann's website, where you find there are four kinds of switch. The Java™ Tutorials are out of date and only show one kind of switch. A switch expression with -> allowed me to reduce your sixty lines to twelve. Simpler than what FH showed us.
You don't need a month number; you can go directly from month to season. I shortened the output a little:-
java Seasoninfo.java
Input a month and day:
March 25
25 March is in Spring
Note that a Scanner allows you to write the month and day on the same line and still have them read correctly.
More information available if needed.
I never like workibg with a lot of ifs. A switch with -> wouold be much better. To practise the use of enums, I tried to rewrite the above with just enums. Here goes:
There are three kinds of actuaries: those who can count, and those who can't.
I suspect there are features in your code that OP is unfamiliar with. This is what I tried earlier and edited a little just now:-The class name should have a capital I in.
Note I have got a class with three fields, all final, and with a private constructor, it should be immutable as written.
The static method finds the season name from the other two fields, and is called from the constructor. Remember that methods called from a constructor should always be marked private or final, but usually not both.
Pairs of months names map to a season name, and the other four months map depending on whether you are before or after the 21st. Not quite scientifically accurate, but we are learning programming not astronomy. Note the ?: operator makes the cases much more concise.
In the case of default, the program throws an exception, which has to be wrapped in an extra pair of {...}.
It would be far better not to use Strings,←link but OP probably hasn't heard of enums yet.
and POOF! You're gone! But look, this tiny ad is still here:
Gift giving made easy with the permaculture playing cards