|
#1
|
|||
|
|||
|
Military Time Converter
I have written a class to convert military time to standard time. What I need to do is modify this code so a user can input the military time. I realize I can take out some extraneous code. Thanks.
import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calendar; // Time abstract data type (ADT) definition public class Time { private int hour; // 0 - 23 (attributes) private int minute; // 0 - 59 (attributes) //private int second; // 0 - 59 (attributes) private Date dt; private long offset; int number; // Time constructor initializes each data member to zero. // Ensures all Time objects start in a consistent state. public Time( ) { setOffset(); setTime(0,0); //removed seconds } // set a new Time value hour, minute public void setTime(int h, int m)throws IllegalArgumentException { if(h > 23 || h <0 || m > 59 ||m < 0 ) throw(new IllegalArgumentException()); hour = h; minute = m; dt = new Date(((h*3600)+(m*60))*1000-offset); } void setOffset() { Calendar c = Calendar.getInstance(); offset =c.get(c.ZONE_OFFSET); } // Print time in standard format public void printStandard( ) { System.out.println(new SimpleDateFormat("h:mm a").format(dt)); } public void printMilitary() { System.out.println(new SimpleDateFormat("HHmm").format(dt)); } // Driver to test simple class Time public static void main( String args[ ] ) { Time t = new Time( ); // instantiate object t of class time System.out.print("\nThe initial standard time is "); t.printStandard( ); System.out.print("\nThe initial military time is "); t.printMilitary(); try { System.out.println("Enter time in 24 hour notation:"); t=SavitchIn.readLine();//THIS IS WHAT I NEED TO FIX??? t.setTime(13,47); } catch(IllegalArgumentException e){System.out.println(e);} System.out.print("That is the same as "); t.printStandard( ); System.out.print("That is the same as "); t.printMilitary( ); try{t.setTime(99, 99);} // attempt invalid settings catch(IllegalArgumentException e){System.out.println(e);} t.printStandard( ); } } |
|
#2
|
||||
|
||||
|
Put this line of code the main method:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); Then implement this method for getting user input: /** * Gets user input and returns hours and minutes as an int[2] * @param input the console * @param type type of timestring requested ("military" or "standard"); * @return */ public static int [] getTimeInput(BufferedReader input, String type) { String s1=null, s2=null; try { String ins=input.readLine().trim(); if (type.equals("military")) { s1=ins.substring(0,2); s2=ins.substring(2,4); } else if (type.equals("standard")){ int ix=ins.indexOf(":"); s1=ins.substring(0,ix); s2=ins.substring(ix+1); } return new int [] {Integer.parseInt(s1), Integer.parseInt(s2)}; } catch (NumberFormatException nfe) { System.out.println("That was not an integer"); return null; } catch (IOException ioe) { System.out.println("You can't read this cause your computer just died"); return null; // ehem.... } } then use this in your main method: System.out.println("Enter time in 24 hour notation:"); int [] sT=getTimeInput(input,"standard"); t.setTime(sT[0],sT[1]); t.printStandard( ); t.printMilitary( ); System.out.println("Enter time in military notation:"); int [] mT=getTimeInput(input,"military"); t.setTime(mT[0],mT[1]); t.printStandard( ); t.printMilitary( );
__________________
eschew obfuscation |
|
#3
|
|||
|
|||
|
Thanks. I am incorporating this now and testing to see if it works the way I want.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|