Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.*; public class Project4 { public static void main(String[] arg

ID: 3792297 • Letter: I

Question

import java.util.*;
public class Project4
{
public static void main(String[] args)
{
String again = "";
do
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the 24 hour format time: ");
String time = keyboard.next();
if(time.indexOf(":") == -1 )
{
System.out.println("Invalid Input. Time must be in format of HH:MM");
}
else
{
try
{
int hours =Integer.parseInt(time.substring(0,time.indexOf(":")));
int minutes =Integer.parseInt(time.substring(time.indexOf(":")+1, time.length()));
if(minutes < 00 || minutes > 59)
{
throw new TimeFormatException();
}
else if(hours < 0 || hours > 23)
{
throw new TimeFormatException();
}
else
{
String smin = "00";
String shours = "12";
String s = "AM";
if(hours == 12 && minutes == 0)
{
s = "PM";
System.out.println(hours+":"+smin+" "+ s);
}
else if(hours == 12 )
{
s = "PM";
System.out.println(hours+":"+minutes+" "+ s);
}
else if(hours == 0 && minutes == 00)
{
System.out.println(shours+":"+smin+" "+ s);
}
else if(hours == 0)
{
System.out.println(shours+":"+minutes+" "+ s);
}
else if(hours>11 && minutes == 0)
{
s = "PM";
hours = hours - 12;
System.out.println(hours+":"+smin+" "+ s);
}
else if(hours>11)
{
s = "PM";
hours = hours - 12;
System.out.println(hours+":"+minutes+" "+ s);
}
else if(minutes == 0)
{
System.out.println(hours+":"+smin+" "+ s);
}
else
{
System.out.println(hours+":"+minutes+" "+ s);
}
}
}
catch(Exception e)
{
System.out.println("Invalid Input. Try again");
}
System.out.println("again? y/n");
again = keyboard.next();
}
}
while(again.equalsIgnoreCase("Y"));
System.out.println("Program is exiting");
}
}

I have this code that is supposed to change time from a 24 hour format to a 12 hour format. I am having a hard time getting it to throw an exception for if the minutes are less than 2 digits. Any help would be appreciated, thanks!

Explanation / Answer

public class Project4 {

  
public static void main(String[] args)
{
String again = "";
do
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the 24 hour format time: ");
String time = keyboard.next();
if(time.indexOf(":") == -1 )
{
System.out.println("Invalid Input. Time must be in format of HH:MM");
}
else
{
try
{
int hours =Integer.parseInt(time.substring(0,time.indexOf(":")));
int minutes =Integer.parseInt(time.substring(time.indexOf(":")+1, time.length()));
/*
Modified rom here
Extracted the substring for minutes and checked its length and if it is not equal to 2 throw the exception
*/
String min=time.substring(time.indexOf(":")+1, time.length());
if(min.length()!=2){
throw new TimeFormatException();
}
/*
Modification ends here
*/
if(minutes < 00 || minutes > 59)
{
throw new TimeFormatException();
}
else if(hours < 0 || hours > 23)
{
throw new TimeFormatException();
}
else
{
String smin = "00";
String shours = "12";
String s = "AM";
if(hours == 12 && minutes == 0)
{
s = "PM";
System.out.println(hours+":"+smin+" "+ s);
}
else if(hours == 12 )
{
s = "PM";
System.out.println(hours+":"+minutes+" "+ s);
}
else if(hours == 0 && minutes == 00)
{
System.out.println(shours+":"+smin+" "+ s);
}
else if(hours == 0)
{
System.out.println(shours+":"+minutes+" "+ s);
}
else if(hours>11 && minutes == 0)
{
s = "PM";
hours = hours - 12;
System.out.println(hours+":"+smin+" "+ s);
}
else if(hours>11)
{
s = "PM";
hours = hours - 12;
System.out.println(hours+":"+minutes+" "+ s);
}
else if(minutes == 0)
{
System.out.println(hours+":"+smin+" "+ s);
}
else
{
System.out.println(hours+":"+minutes+" "+ s);
}
}
}
catch(Exception e)
{
System.out.println("Invalid Input. Try again");
}
System.out.println("again? y/n");
again = keyboard.next();
}
}
while(again.equalsIgnoreCase("Y"));
System.out.println("Program is exiting");
}
}