Changing Delimiter to Convert Military Time to Civilian Time

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

Discussion Overview

The discussion revolves around converting military time (24-hour format) to civilian time (12-hour format) in a programming context, specifically focusing on issues related to changing the delimiter in input handling within a Java program.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to use a colon as a delimiter for input but encounters issues with the program's functionality.
  • Another participant asks for clarification on how the military time is being entered, suggesting that using integers instead of strings might resolve some issues.
  • A participant mentions that they are entering the time as "14:00" and reports an InputMismatchException when trying to use nextInt after changing variable types.
  • Another participant proposes that the delimiter may not be capturing whitespace at the end of the input and suggests using a regular expression to delimit both the colon and whitespace.

Areas of Agreement / Disagreement

Participants express differing views on how to handle the input and delimiters, indicating that there is no consensus on the best approach to resolve the issue.

Contextual Notes

There are unresolved assumptions regarding input formatting and handling of whitespace, which may affect the program's behavior.

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
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
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