Question:
Need help with this Java program , i'm a rookie please help?
Bill
2010-01-31 19:23:10 UTC
I'm suppose to

" 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);
}
}
}
Three answers:
JavaIntermediate
2010-02-01 13:19:51 UTC
import java.text.SimpleDateFormat;

import java.util.Scanner;

import java.util.Date;

public class TimeThrowable {

TimeThrowable() {

boolean moretimes=true;

while(moretimes){

System.out.println("Please Enter a Time (q to exit): ");

Scanner sc=new Scanner(System.in);

String s=sc.next();

if(s.equals("q")){System.exit(0);}

SimpleDateFormat sdf=new SimpleDateFormat("h:mm a");

try{

Date cur=new Date("Mon Feb 01 "+s+" CST 2010");



System.out.println(sdf.format(cur));

}catch(Exception ex){

System.out.println(ex);

System.out.println(s+"is not a valid Time");

}

}



}

public static void main(String[] args){

new TimeThrowable();



}

}
Blackcompe
2010-02-01 03:52:32 UTC
Umm.......so declaring a try/catch block is easy right:



try{

//code that can throw someException

}catch(someException){}



So, basically what you want to do is 'throw' the WrongTimeException from within the try block, by either doing:



try {



if(some condition) { //jebberish time

throw WrongTimeException();

}else {



}

}catch(WrongTImeException e){ System.out.println("Invalid Time"); }



or



try {

getTime(); //this method will throw the exception

}catch(WrongTImeException e){ System.out.println("Invalid Time"); }



void getTime() { if(some condition) { throw WrongTimeException(); } }



I prefer the second example, because it best shows the most common usage of try/catch blocks. Generally you create a try/catch block around some code that may produce an exception; the catch block handles it and your code continues without problems. Either one will work.



The Exception class you have to write can simply be:



class WrongTimeException extends Exception {}



That's it. By extending Exception, you've got an subclass with the functionality of an Exception object. You don't need to worry about anything else. You should be familiar with subclasses at this point though.



You could even start out by throwing an Exception object to get the feel for it.





try {



if(some condition) { //jebberish time

throw Exception();

}else {



}

}catch(Exception e){ System.out.println("Invalid Time"); }



Exception is pre-defined Java class, right? So you can use that initially.



And, to convert those times, well, it's just some simple algebra. You don't need to use any Java classes for that (Calendar, Date).
Pete S
2010-02-01 03:36:04 UTC
A couple of things, first you need to define doesn't work, for me, this example code doesn't compile.



The reason it doesn't compile is that is lacks the import java.util.Scanner; statement so the compiler knows where to find the Scanner class.



So after adding that, I saw no glaring wrong inputs initially.



However, there's a small problem with your program design too, you've placed all of your code in what is called a class constructor. Typically this constructor method should only contain member variable initializations for your class. It would be better to place the code in a method named something like

public void print24HourFormatAsStandardFormat(String time) {

//Code here

}

even better is

public String format24HourFormatAsStandardFormat(string time) {

String result = "";

//Conversion code

return result;

}

Then you could use this method without caring if its going into System.out or a P2P application you write later ;).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...