0

So I've got my code working so far, but I've got two problems. this code lets you put in a month, day, and year and it tells you how many days it has. essentially it's a leap-year checker but includes every month (for whatever reason).

Right now the only way I know to make cases work is to use numbers, but I want the user to be able to type the actual name of the month as input and the code still knows which case to take it to. when I try to name the case month names it says it doesn't recognize the variable. so that's my first problem.

My second problem is if the user tries to input characters into the "Year" section that aren't integers, I want it to give an error message. someone told me to use .hasNextInt() but I'm totally unsure of the functionality of it or how to implement it. I've looked at other people's code on here trying to figure out how it's used to no avail. here's my code, any suggestions or guidance would be greatly appreciated. thank you in advance!

public class MonthLength {
  public static void main(String[] args) {
    // Prompt the user to enter a month
    SimpleIO.prompt("Enter a month name: ");
    String userInput = SimpleIO.readLine();
    int month = Integer.parseInt(userInput);

    // Terminate program if month is not a proper month name
    if (month < 1 || month > 12) {
      System.out.println("Illegal month name; try again");
      return;
    }

    // Prompt the user to enter a year
    SimpleIO.prompt("Enter a year: ");
    userInput = SimpleIO.readLine();
    int year = Integer.parseInt(userInput);

    //Terminate program if year is not an integer
    if (year < 0) {
       System.out.println("Year cannot be negative; try again");
       return;
    }

    // Determine the number of days in the month
    int numberOfDays;
    switch (month) {
      case 2:  // February
               numberOfDays = 28;
               if (year % 4 == 0) {
                 numberOfDays = 29;
                 if (year % 100 == 0 && year % 400 != 0)
                   numberOfDays = 28;
               }
               break;

      case 4:  // April
      case 6:  // June
      case 9:  // September
      case 11: // November
               numberOfDays = 30;
               break;

      default: numberOfDays = 31;
               break;
    }

    // Display the number of days in the month
    System.out.println("There are " + numberOfDays +
                       " days in this month");
  }
}
Rayshawn
  • 2,603
  • 3
  • 27
  • 46
Evan Lemmons
  • 807
  • 3
  • 11
  • 27

3 Answers3

0

prior java 7 versions don't allow String as an argument in switch. in java 7 you can pass String as an argument for switch.

you can use Enum here.

enum Months{
JAN,FEB,MAR,APR}
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • Yes, but the user enters the string. You should give more information on how to to make it work: `switch (Months.valueOf(month)) { ... }`. – Axel Nov 02 '12 at 21:45
0
  1. For your first problem you can use enum How is values() implemented for Java 6 enums?

  2. For your second problem, you can ask the user to input a String and use Integer.parseInt(String) to convert it to int so you wont have to perform any checks. Hope that helps

Community
  • 1
  • 1
noMAD
  • 7,744
  • 19
  • 56
  • 94
0

Chaitanya10 has detailed why your first issue is the way it is. If you need to do comparisons on Strings (earlier java ver) you'll need to build an if-else block:

if (monthInput.toLower().equals("january") {
  month = 1;
} else if (monthInput.toLower().equals("february") {
  ....etc....
}

For your second issue I would capture the user input as a String and then use a check to make sure it was a number they entered. Create a function like this:

public boolean isNumeric(String input) {
  try {
    Integer.parseInt(input);
    return true;
  } catch (NumberFormatException numForEx) {
    return false;
  }
}

If that returns true you can safely convert the String to an integer:

year = Integer.parseInt( input );
Grambot
  • 4,370
  • 5
  • 28
  • 43
  • 1
    While this works, you parse each String two times. How about changing to something like `public Integer getNumVal(String input) { try { return Integer.parseInt(input); } catch (NumberFormatException numForEx) { return null; } }` and then doing your error handling for null return values? – Axel Nov 02 '12 at 21:50
  • i tried building the if else block with this format: `code SimpleIO.prompt("Enter a month name: "); String userInput = SimpleIO.readLine(); int month = Integer.parseInt(userInput);if (userInput.trim().toLowerCase().equals("january")) { month = 1; } else if (userInput.trim().toLowerCase().equals("february")) { month = 2;` etc. all the way to december, and it compiles just fine but when it's run i get error messages when i try to input the month name. including "java.lang.numberFormatException" and "java.land.integer.parseInt(UnknownSource)" any advice? – Evan Lemmons Nov 03 '12 at 14:37
  • Could you post your code to a pastebin link? I imagine you just have a bad datatype declaration or something simple that we could resolve with a look at your code – Grambot Nov 05 '12 at 14:23