Question: I keep getting an error when I run my java code. I'm sure it's something simple, but I can't seem to figure it out -
I keep getting an error when I run my java code. I'm sure it's something simple, but I can't seem to figure it out - it has to do with the correct output of a two-digit day without leading zeros. My code outputs leading zeros. I need to correct this. Thanks!
My Java code:
import java.util.Scanner; import java.util.*; import java.io.*; import java.text.*; import java.text.SimpleDateFormat; import java.util.Calendar; public class MonthDayConverter { String startDate; int slash = 0; String m; String d; String monthDay; int monthNum = 0; int dayNum = 0; int days = 0; public void userInput() { try { System.out.print("Enter date in the format mm/dd:"); Scanner scn = new Scanner (System.in); startDate = scn.nextLine(); slash = startDate.indexOf('/'); if (slash != 2) throw new Exception(); m = startDate.substring (0, slash); d = startDate.substring (3); monthNum = Integer.parseInt (m); dayNum = Integer.parseInt (d); } catch (Exception ex) { System.out.println(ex.getMessage()); System.exit(0); } } public void isValidMonth() { try { if ((monthNum < 0) || (monthNum > 12)) throw new MonthException(); } catch (MonthException e) { System.out.println(e.getMessage()); System.exit(0); } } public void isValidDay() { try { switch (monthNum) { case 1: if (monthNum == 1) monthDay = "January"; if (dayNum > 31) throw new DayException(); break; case 2: if (monthNum == 2) monthDay = "February"; if (dayNum > 28) throw new DayException(); break; case 3: if (monthNum == 3) monthDay = "March"; if (dayNum > 31) throw new DayException(); break; case 4: if (monthNum == 4) monthDay = "April"; if (dayNum > 30) throw new DayException(); break; case 5: if (monthNum == 5) monthDay = "May"; if (dayNum > 31) throw new DayException(); break; case 6: if (monthNum == 6) monthDay = "June"; if (dayNum > 30) throw new DayException(); break; case 7: if (monthNum == 7) monthDay = "July"; if (dayNum > 31) throw new DayException(); break; case 8: if (monthNum == 8) monthDay = "August"; if (dayNum > 31) throw new DayException(); break; case 9: if (monthNum == 9) monthDay = "September"; if (dayNum > 30) throw new DayException(); break; case 10: if (monthNum == 10) monthDay = "October"; if (dayNum > 31) throw new DayException(); break; case 11: if (monthNum == 11) monthDay = "November"; if (dayNum > 30) throw new DayException(); break; case 12: if (monthNum == 12) monthDay = "December"; if (dayNum > 31) throw new DayException(); break; default: break; } } catch (DayException e) { System.out.println(e.getMessage()); System.exit(0); } } public void display() { System.out.println(monthDay+" "+d); } public static void main (String args[]) { try { MonthDayConverter alphaMonth = new MonthDayConverter(); alphaMonth.userInput(); alphaMonth.isValidDay(); alphaMonth.isValidMonth(); alphaMonth.display(); } catch (Exception ex) { System.out.println("Exception" +ex.getMessage()); } } } class MonthException extends Exception { public MonthException() { super ("Invalid month number!"); } public MonthException (String message) { super (message); } } class DayException extends Exception { public DayException() { super ("Invalid day number!"); } public DayException (String message) { super (message); } }
_________________________________________________________________________________________________
This is the error message I am getting:
CODELAB ANALYSIS: LOGICAL ERROR(S)
Problems Detected: The contents of your standard output is incorrect.
Given the following was entered from the keyboard: 01/01 you displayed: Enter date in the format mm/dd:January 01 instead of: Enter date in the format mm/dd:January 1
Failed 1 out of 7 test runs.
Failed Test Run
The contents of your standard output is incorrect.
Expected Result:
Enterdateintheformatmm/dd:
01/01
January1
Your Code's Actual Result:
Enterdateintheformatmm/dd:
01/01
January01
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
