• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to input a month then a day then it will print the season. However it just prints null. What am I doing wrong here?
Staff note (Liutauras Vilda) :

Please try to choose a more descriptive / elaborate topic name than just "Help"

 
Saloon Keeper
Posts: 10928
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 4639
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the shape of your code when properly formatted - I'm sure this is not what you are wanting.
 
Carey Brown
Saloon Keeper
Posts: 10928
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is inconsistently indented making it difficult to follow. This is how it should look:
 
Carey Brown
Saloon Keeper
Posts: 10928
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you join your else-if's together you reduce some of your indentation.
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it just prints null. What am I doing wrong here?


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.
 
Marshal
Posts: 79952
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Saloon Keeper
Posts: 28317
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some programming languages have an actual "elseif" or "elif" statement. Java, alas, does not. But I do like Carey does and it's close enough.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would add the month info to the print:
 
Campbell Ritchie
Marshal
Posts: 79952
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5551
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Campbell Ritchie
Marshal
Posts: 79952
396
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic