How to put an Invalid Month output?

sonk

New Member
Date Input from month 13,14,15 and above are continuously making an Zodiac Sign output, it should be an invalid month. How can i fix it?======================================================================================\[code\]import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Scanner;public class MainTab{ public String[] chineseZodiac = new String[] { "Monkey","Rooster","Dog","Pig","Rat","Ox","Tiger","Rabbit","Dragon", "Snake","Horse","Sheep" }; public String[] zodiacSigns = new String[] { "Capricorn","Aquarius","Pisces","Aries","Taurus","Gemini", "Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius" }; public static void main(String[] args) { Scanner s = new Scanner(System.in); MainTab x = new MainTab(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); System.out.printf("Enter a date (MM/dd/yyyy): "); String input = s.nextLine(); Calendar cal = Calendar.getInstance(); try { Date date = sdf.parse(input); cal = new GregorianCalendar(); cal.setTime(date); } catch (ParseException pe) { pe.printStackTrace(); } System.out.printf(" Your Chinese Zodiac Sign is: %s\n", x.getChineseZodiac(cal.get(Calendar.YEAR))); System.out.printf(" Your Zodiac Sign is: %s\n", x.getZodiac(cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH))); } public String getZodiac(int month, int day) { if((month == 11) && ( day>= 22) || (month == 0) && (day <= 19)) { return zodiacSigns[0]; }else if((month == 0) && (day >= 20) || (month == 1) && (day <= 18)) { return zodiacSigns[1]; } else if((month == 1) && (day >= 19) || (month == 2) && (day <= 20)) { return zodiacSigns[2]; } else if((month == 2) && (day >= 21) || (month == 3) && (day <= 19)) { return zodiacSigns[3]; } else if((month == 3) && (day >= 20) || (month == 4) && (day <= 20)) { return zodiacSigns[4]; } else if((month == 4) && (day >= 21) || (month == 5) && (day <= 20)) { return zodiacSigns[5]; } else if((month == 5) && (day >= 21) || (month == 6) && (day <= 22)) { return zodiacSigns[6]; } else if((month == 6) && (day >= 23) || (month == 7) && (day <= 22)) { return zodiacSigns[7]; } else if((month == 7) && (day >= 23) || (month == 8) && (day <= 22)) { return zodiacSigns[8]; } else if((month == 8) && (day >= 23) || (month == 9) && (day <= 22)) { return zodiacSigns[9]; } else if((month == 9) && (day >= 23) || (month == 10) && (day <= 21)) { return zodiacSigns[10]; } else if((month == 10) && (day >= 23) || (month == 11) && (day <= 21)) { return zodiacSigns[11]; // } else if((month == 13) || (month == 14) || (month == 15)) {// return zodiacSigns[12]; } return null; } public String getChineseZodiac(int year) { return chineseZodiac[year%12]; } }\[/code\]=====================================================================================
 
Back
Top