I am trying to write a program that will get a date from the user and the user can enter the month
- as a
String
("july"
) - or as a number (
7
).
The code works fine when the user gives an integer for the month but when the user gives the month as a string the program says:
Exception in thread "main" java.util.InputMismatchException
and then ends. I think theres a syntax error or something in the main method but I am not sure...
Sorry I included so much of my code... I don't really know where the problem is.
import java.util.*;
public class DateHoliday{
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
//declare varaiables for month, day and year
int monthNumber, day, year;
String, monthString, holiday;
System.out.print("This program will ask you for a month, day, and year\n");
System.out.print("and will print the corresponding date in two standard date formats.\n");
int repeat = kb.nextInt();
//ask the user how many times to run the code
System.out.print("How many times do you want to run the code:");
for (int i = 1; i <= repeat; i++){
//prompt the user to enter the month
System.out.print("Please note, you may enter the month as:\nA numeric value (1-12)\n");
System.out.print("an unabbreviated month name (January etc....)\nEnter the month:");
//check to see if the user is entering an integer for the month
if(kb.hasNextInt()) {
//read the month as an integer
monthNumber = kb.nextInt();
//call the method to convert the month to a string
getMonthString(monthNumber);
} else {
//read the month as a String
monthString = kb.nextLine();
//call the method to convert it to an integer
monthNumber = getMonthNumber(monthString);//I THINK THIS IS THE PROBLEM
}
//get the day from the userS ;
System.out.print("Enter the day:");
day = kb.nextInt();
//get the year from the user;
System.out.print("Enter the year:");
year = kb.nextInt();
// call the method to get the holidays assosiated with the given date
// display the desired output in the given format
holiday = getHoliday(monthNumber, day, year);
System.out.println("The Date is: FIXMONTH/"+day+"/"+year+" FIXMONTH "+day+", "+year+" "+holiday);
}
}
public static String getMonthString(int monthNumber) {
String result = "";
switch(monthNumber) {
case 1: result = "january"; break;
case 2: result = "february"; break;
case 3: result = "march"; break;
case 4: result = "april"; break;
case 5: result = "may"; break;
case 6: result = "june"; break;
case 7: result = "july"; break;
case 8: result = "august"; break;
case 9: result = "september"; break;
case 10: result = "october"; break;
case 11: result = "november"; break;
case 12: result = "december"; break;
}
return result;
}
public static int getMonthNumber(String monthString) {
String lowerMonth = monthString.toLowerCase();
int result = 0;
switch(lowerMonth) {
case "january": result = 1; break;
case "february": result = 2; break;
case "march": result = 3; break;
case "april": result = 4; break;
case "may": result = 5; break;
case "june": result = 6; break;
case "july": result = 7; break;
case "august": result = 8; break;
case "september": result = 9; break;
case "october": result = 10; break;
case "november": result = 11; break;
case "december": result = 12; break;
}
return result;
}
Thanks so much! I actually needed kb.nextLine(); after monthString = kb.nextLine(); but i would not of figured that out without you guys! Im going to see if i can work out how to add the months to the output correctly on my own....wish me luck