Changing Delimiter to Convert Military Time to Civilian Time

  • Thread starter major_maths
  • Start date
In summary, the person is having trouble converting military time to civilian time and is using a program to do so. They have tried different methods but are still unable to change the delimiter to ":". They have also tried using an int to store the values instead of a string, but the program is not working properly. They may need to use a regular expression to delimit the whitespace character in addition to ":".
  • #1
major_maths
30
0
I'm trying to write a program that converts military time to civilian time (24-hour to the standard 12-hour) but I'm having trouble with changing the delimiter to ":". I've tried re-working the code every way I can think of and it still won't work properly.

Code:
import java.util.Scanner;
public class Program4
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);		
		keyboard.useDelimiter(":");
		String theHour, theMin;

		System.out.println("Enter time in 24-hour notation.");
		theHour=keyboard.next();
		theMin=keyboard.next();
		
		System.out.println("The time is "+theHour+":"+theMin);
	}
}
 
Physics news on Phys.org
  • #2
How are you entering the military time? Say, if it's 2 PM, are you entering 14:00 or 1400?
If you are entering it as 14:00, I would suggest using an int to store your values rather than a string,
then use nextInt instead of next.
 
Last edited:
  • #3
I'm entering it like 14:00. I tried changing the variables to integers and used nextInt, but the program waits for me to do something else after that. I tried entering 14:00 again after that just to try something and got an InputMismatchException, if that helps.
 
  • #4
It looks like the delimiter is not catching the whitespace at the end of the string that you entered. You need to delimit two things: the : and the whitespace character \\s by using a regular expression.
 
Last edited:
  • #5


Hello,

Thank you for sharing your issue with converting military time to civilian time. Changing the delimiter to ":" can be a helpful step in this process, but there may be other factors that are causing the code to not work properly.

One suggestion I have is to check the input format of the military time being entered. Make sure it follows the 24-hour notation with two digits for the hour and two digits for the minutes, separated by a colon. For example, 13:30 would represent 1:30 PM in civilian time.

Another thing to consider is the data type being used to store the time. It may be helpful to use an integer or double data type to store the hour and minute values separately, and then convert them to strings with the appropriate formatting (e.g. adding a "0" in front of single digit numbers).

I hope these suggestions help in troubleshooting your code. If you continue to experience issues, feel free to reach out for further assistance. Keep up the good work in your programming journey!
 

FAQ: Changing Delimiter to Convert Military Time to Civilian Time

What is military time?

Military time, also known as 24-hour time, is a timekeeping system where the day is divided into 24 hours, starting at midnight (00:00) and ending at 23:59. This system is commonly used in the military and other professions that require precise timekeeping.

How is military time different from civilian time?

Military time differs from civilian time in the way it is displayed. While civilian time follows the 12-hour clock format with AM and PM, military time uses the 24-hour clock format without any AM or PM designations. Additionally, military time does not use a colon to separate hours and minutes, and the hours are not preceded by a zero.

How do you convert military time to civilian time?

To convert military time to civilian time, you can simply subtract 12 from the hours if it is after 12 PM (noon). For example, 14:00 military time would become 2:00 PM civilian time. If the military time is before 12 PM, it remains the same in civilian time. For example, 08:00 military time would be 8:00 AM civilian time.

How can I change the delimiter when converting military time to civilian time?

To change the delimiter when converting military time to civilian time, you can simply replace the colon with a space. For example, instead of displaying 15:30 as 3:30 PM, you can display it as 3 30 PM.

Why is it important to convert military time to civilian time?

Converting military time to civilian time is important for those who are not familiar with the 24-hour clock format. It allows for easier understanding and communication of time between individuals who may use different timekeeping systems. Additionally, many digital devices and clocks use the 12-hour clock format, so converting military time to civilian time can make it easier to set and read these devices.

Similar threads

Replies
7
Views
2K
Replies
1
Views
2K
Replies
7
Views
2K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
1
Views
2K
Replies
12
Views
2K
Replies
1
Views
3K
Back
Top