Please don't be mean! Amateur question!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • majik1213
    New Member
    • Sep 2007
    • 4

    #1

    Please don't be mean! Amateur question!

    Greetings!
    I'm taking a Java course and I'm working on a *simple* program and have run into a rut that I hope you can help me resolve! This program basically asks you what month you were born and then, for the purposes of a future calculation, it converts the month you type into an integer value for that month (e.g. December->12). But this step is what I cannot perform. Here is the portion of the code in question, any suggestions?

    String birthMonth=JOpt ionPane.showInp utDialog("what month were you born?");
    if (birthMonth.equ als("January"))
    {
    birthMonth = new Integer("1");
    }

    as you can see, I'm clearly trying to tell the computer that if the user enters january, then redefine that variable string they entered into a new integer numbered 1. BlueJ does not like this, because I've already defined birthmonth. So any suggestions?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You should have 2 variables - let one be String birthmonth, and get it's value with the JOptionPane.sho wInputDialogue as you have already. Let the second be int birthMonthVal, and use this to hold the numerical value. So, if the user enters "January," put 1 into birthMonthVal.

    As an aside, you may want to try a different method rather than .equals for your comparison. Why? Well, your program will work fine if the user enters "January" but what about "january" or "JANUARY"? Clearly, these responses are both 'equal' to "January" within the context of your problem, but your program won't pick them up.

    Hint: Do a google search for String.java and look at some of the methods that appear in the first response.

    Comment

    • majik1213
      New Member
      • Sep 2007
      • 4

      #3
      Originally posted by Ganon11
      You should have 2 variables - let one be String birthmonth, and get it's value with the JOptionPane.sho wInputDialogue as you have already. Let the second be int birthMonthVal, and use this to hold the numerical value. So, if the user enters "January," put 1 into birthMonthVal.

      As an aside, you may want to try a different method rather than .equals for your comparison. Why? Well, your program will work fine if the user enters "January" but what about "january" or "JANUARY"? Clearly, these responses are both 'equal' to "January" within the context of your problem, but your program won't pick them up.

      Hint: Do a google search for String.java and look at some of the methods that appear in the first response.
      WOW THANKS!!!!!!!!! !!!!!!!!!!!!!!! !!!!!!!!!!

      Comment

      Working...