Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Problem11_BeerTime {
- public static void main(String[] args) {
- Scanner myScan = new Scanner(System.in);
- String time = myScan.nextLine();
- String result = "";
- int index = time.indexOf(":");
- int indexSp = time.indexOf(" ");
- if (index < 0 || indexSp < index) {
- result = "invalid time";
- } else {
- String hourAsString = time.substring(0, index);
- String minutesAsString = time.substring(index + 1, indexSp);
- String am_pm = time.substring(indexSp + 1);
- if (hourAsString.length() > 2 || minutesAsString.length() != 2 || !(am_pm.equals("PM") || am_pm.equals("AM"))) {
- result = "invalid time";
- } else {
- try {
- int hour = Integer.parseInt(hourAsString);
- int minutes = Integer.parseInt(minutesAsString);
- if (hour < 1 || hour > 12 || minutes < 0 || minutes > 59) {
- result = "invalid time";
- } else if (hour > 0 && hour < 12 && am_pm.equals("PM") || (hour == 12 || hour > 0 && hour < 3) && am_pm.equals("AM")) {
- result = "beer time";
- } else {
- result = "non-beer time";
- }
- } catch (Exception exception) {
- result = "invalid time";
- }
- }
- }
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment