0

I am creating a program that converts Standard Time to the 24-hour-clock. Unfortunately for me, my "if" statements when the user inputs an incorrect time do not seem to be working properly.

Here is my code (I have included all of it because I feel like this error may be caused by another lower line):

public class StandardTime {

    static String amPM;//will hold am/pm for standard time 
    static String traditionalTime;//will store traditional time from user
    static int hours;//will store hours and minutes
    static int min1, min2;

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// user input

        int tryAgain = 1;//will initial do-while loop
        System.out.println("Standard Time to Traditional Time Converter");
        System.out.println("===========================================");
        do {
            System.out.println();
            System.out.println("Input a time in Standard Form (HH:MM:SS):");
            String standardTime = br.readLine();//user inputs time in standard form
            System.out.println();
            String minute1 = standardTime.substring(3);
            min1 = Integer.parseInt(minute1);
            String minute2 = standardTime.substring(5);
            min2 = Integer.parseInt(minute2);
            do {
                if (standardTime.length() != 8) {
                    System.out.println("Invalid time entered.");
                    System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
                    standardTime = br.readLine();//user inputs time in standard form
                    System.out.println();
                }
                else if (min1 >= 6)
                {
                    System.out.println("Invalid time entered. Please do not input a minute time over 59.");
                    System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
                    standardTime = br.readLine();//user inputs time in standard form
                    System.out.println();
                }
            } while ((standardTime.length()) != 8 || (min1 >= 6));
            //method declaration
            convertToTraditional(standardTime); // call the coversion method
            System.out.println(standardTime + " is equivalent to " + hours + ":" +min1+min2+ " " + amPM + ".");
            System.out.println();
            System.out.println("Enter 1 to try again or any other number to exit the program.");
            tryAgain = Integer.parseInt(br.readLine());//user decides to try again
        } while (tryAgain == 1);//will repeat if user enters 1

    }//closes main body

    public static void convertToTraditional(String standardTime) {
        String hour = standardTime.substring(0, 2); //captures substring of hours in user's input
        hours = Integer.parseInt(hour); //converts substring to INT value
        int pmHour = hours;
        if (12 == hours) { //creates a statement for 12PM (noon)
            hours = pmHour;
            amPM = "PM";
        } else if (hours > 12 && hours <= 23) { //if the time is not equal to noon or midnight
            hours = hours - 12;
            amPM = "PM";
        } else if (hours == 0) { //12 midnight
            hours = hours + 12;
            amPM = "AM";
        } else if (hours == 24) { //also another way to express 12 midnight on the 24 hour clock
            hours = hours - 12;
            amPM = "AM";
        } else { //else if the hours are between midnight and 11am
            hours = pmHour;
            amPM = "AM";
        }
    }
}

The error specifically comes up on the if "standardTime.length() != 8." I want the user to type it in using the following format "HH:MM:SS". This is the error I receive when, for example, I type in 1:2:3...

Exception in thread "main" java.lang.NumberFormatException: For input string: ":3"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)

    at standardtime.StandardTime.main(StandardTime.java:35)
Java Result: 1

Thank you for any help.

Aarden Hartle
  • 39
  • 1
  • 4
  • 10
  • It looks more like one of the `parseInt()`s is throwing. Shouldn't you do the `substring()` and `parseInt()` bits *after* checking `standardTime.length()`? – Biffen Nov 27 '14 at 14:44
  • 1
    Do `01:02:03` instead as your input. It should precisely follow your instructions. Also, parse out the `:` before attempting to convert a `String` to an `int`. – But I'm Not A Wrapper Class Nov 27 '14 at 14:45
  • @AardenHartle Moreover, you have a lot of duplicated code and doing a lot of parsing that Java could do for you. Perhaps a re-thinking is in order? – Biffen Nov 27 '14 at 14:47
  • @Biffen It could also be possible that this is a homework assignment which he/she is not allowed to use the `Date` class (or any other class). – But I'm Not A Wrapper Class Nov 27 '14 at 14:49
  • It is a homework assignment and the Date class is not allowed. Thank you for your points. I will do some rewriting. – Aarden Hartle Nov 27 '14 at 14:50
  • Try standardTime.split(":"). This will help you simplify parsing which is the cause of your error. See [this](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Ioannis Deligiannis Nov 27 '14 at 14:52

2 Answers2

0

The error comes when you try to parse ":3" as a number, when it's got the colon, then it's not a number and it throws this exception...

Why don't you try to use java.util.Date ? Your code would be cleaner and easier to write if you use it!

JorgeGRC
  • 1,032
  • 2
  • 18
  • 37
0

Your Problem is not the mentioned if statement, it is this code:

String standardTime = br.readLine();//user inputs time in standard form
System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);

You're reading the input from the user and you're use it immediately and therefore before you check the length of that input.

Move the do/while loop between the above lines to fix that:

String standardTime = br.readLine();//user inputs time in standard form

do {
    // check
} while ((standardTime.length()) != 8 || (min1 >= 6));

System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);
Tom
  • 16,842
  • 17
  • 45
  • 54