Changing Delimiter to Convert Military Time to Civilian Time

  • Thread starter Thread starter major_maths
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on converting military time to civilian time in Java, specifically addressing issues with delimiters in user input. The user attempted to use the Scanner class with a colon (":") as a delimiter but encountered problems when entering time in the format "14:00". The solution involves using a regular expression to delimit both the colon and whitespace, allowing for proper parsing of the input. The suggested code modification includes using `keyboard.useDelimiter(":\\s*")` to handle the input correctly.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of the Scanner class in Java
  • Regular expressions in Java
  • Basic knowledge of military and civilian time formats
NEXT STEPS
  • Learn about Java regular expressions for input parsing
  • Explore the Scanner class methods in Java documentation
  • Study error handling in Java, specifically InputMismatchException
  • Investigate alternative methods for time conversion in Java
USEFUL FOR

Java developers, programming students, and anyone interested in handling user input and time conversion in Java applications.

major_maths
Messages
30
Reaction score
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
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:
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.
 
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:

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K