Bill
2010-01-31 19:23:10 UTC
" Write a program, using try-catch block that converts from 24-hour time to 12-hour time. The following are sample dialogs. The bold faced numbers are entered by the user from the keyboard.
You need to define an exception called WrongTimeException. If the user enters a wrong time, like 34 25 or even gibberish like ab$cd& your program should throw an catch a WrongTimeException. "
-------------------------------------------------------------------------- i did whats below so far. it's not working tho .
public class Project1 {
Project1(String time){
if(time.equals("q")){
System.exit(0);
}
if(time.contains(":")){
time=time.substring(0,time.
indexOf(":")).
concat(time.
substring(time.indexOf(":")+1));
System.out.println(time);
}
int t=Integer.parseInt(time);
time=String.valueOf(t);
if(t<100){
System.out.println("The time is 12:"+t+" AM");
}
else if(t<1200){
if(time.length()==3)
System.out.println(time.
substring(0,1).concat(":").
concat(time.
substring(1,time.length())).
concat(" AM"));
if (time.length()==4)
System.out.println(time.
substring(0,2).concat(":").
concat(time.
substring(2,time.length())).
concat(" AM"));
}
else if(t<1300){
System.out.println(time.
substring(0,2).concat(":").
concat(time.
substring(2,time.
length())).
concat(" PM"));
}else{
t=t-1200;
time=String.valueOf(t);
if(time.length()==3){
System.out.println(String.valueOf(t).
substring(0,1).concat(":").
concat(time.
substring(1,time.length())).
concat(" PM"));
}
if (time.length()==4){
System.out.println(time.
substring(0,2).
concat(":").
concat(time.
substring(2,time.length())).
concat(" PM"));
}
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter a 24 hour time(q to exit): ");
while(true)
try{
new Project1(sc.next());
}catch(Exception ex){
System.out.println("this is what happened");
System.out.println(ex);
}
}
}