Read in a time such as 3 pm and print the equivalent military hour (such as 15). Validate the input. If the input doesn’t start with an integer, print: Error: Not an integer. If the number isn’t between 1 and 12, print: Error: The hour must be between 1 and 12. If the suffix isn’t "am" or "pm", print: Error: The suffix must be am or pm.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 21PE
icon
Related questions
Question

Read in a time such as 3 pm and print the equivalent military hour (such as 15). Validate the input.

  • If the input doesn’t start with an integer, print: Error: Not an integer.
  • If the number isn’t between 1 and 12, print: Error: The hour must be between 1 and 12.
  • If the suffix isn’t "am" or "pm", print: Error: The suffix must be am or pm.

 

 

## Converting Standard Time to Military Time Using Java

In this lesson, you will learn to write a Java program that reads in a time such as "3 pm" and prints the equivalent military hour (such as "15"). The program includes input validation to ensure that users input the time correctly.

### Requirements
- **Valid Input**: The program expects an integer followed by either "am" or "pm".
- **Input Validation**: 
  - If the input doesn’t start with an integer, prints: `Error: Not an integer.`
  - If the number isn’t between 1 and 12, prints: `Error: The hour must be between 1 and 12.`
  - If the suffix isn’t "am" or "pm", prints: `Error: The suffix must be am or pm.`

### TimeReader.java

```java
import java.util.Scanner;

public class TimeReader
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        
        if (in.hasNextInt())
        {
            int hour = in.nextInt();

            if (hour >= 1 && hour <= 12)
            {
                String suffix = in.next();

                if (suffix.equals("am") || suffix.equals("pm"))
                {
                    // Convert hour to military time
                    if (suffix.equals("pm") && hour != 12)
                    {
                        hour += 12;
                    }
                    else if (suffix.equals("am") && hour == 12)
                    {
                        hour = 0;
                    }

                    System.out.println(hour);
                }
                else
                {
                    System.out.println("Error: The suffix must be am or pm.");
                }
            }
            else
            {
                System.out.println("Error: The hour must be between 1 and 12.");
            }
        }
        else
        {
            System.out.println("Error: Not an integer.");
        }
    }
}
```

### Explanation

1. **Importing Scanner**:
   ```java
   import java.util.Scanner;
   ```
   The `Scanner` class is imported to allow reading user input from the console.

2. **Class Definition**:
   ```java
   public class TimeReader
   ```
   The class `TimeReader` is defined to encapsulate the program logic.

3. **Main Method**:
   ```java
   public static void main(String[]
Transcribed Image Text:## Converting Standard Time to Military Time Using Java In this lesson, you will learn to write a Java program that reads in a time such as "3 pm" and prints the equivalent military hour (such as "15"). The program includes input validation to ensure that users input the time correctly. ### Requirements - **Valid Input**: The program expects an integer followed by either "am" or "pm". - **Input Validation**: - If the input doesn’t start with an integer, prints: `Error: Not an integer.` - If the number isn’t between 1 and 12, prints: `Error: The hour must be between 1 and 12.` - If the suffix isn’t "am" or "pm", prints: `Error: The suffix must be am or pm.` ### TimeReader.java ```java import java.util.Scanner; public class TimeReader { public static void main(String[] args) { Scanner in = new Scanner(System.in); if (in.hasNextInt()) { int hour = in.nextInt(); if (hour >= 1 && hour <= 12) { String suffix = in.next(); if (suffix.equals("am") || suffix.equals("pm")) { // Convert hour to military time if (suffix.equals("pm") && hour != 12) { hour += 12; } else if (suffix.equals("am") && hour == 12) { hour = 0; } System.out.println(hour); } else { System.out.println("Error: The suffix must be am or pm."); } } else { System.out.println("Error: The hour must be between 1 and 12."); } } else { System.out.println("Error: Not an integer."); } } } ``` ### Explanation 1. **Importing Scanner**: ```java import java.util.Scanner; ``` The `Scanner` class is imported to allow reading user input from the console. 2. **Class Definition**: ```java public class TimeReader ``` The class `TimeReader` is defined to encapsulate the program logic. 3. **Main Method**: ```java public static void main(String[]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning