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.
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.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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[]](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fd772767f-fd91-4ca5-b737-b5766e3fef0a%2F307190e2-5c70-4875-9215-8a5b95cb3a18%2Fsxahh9w.png&w=3840&q=75)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images

Knowledge Booster
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.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education