Changing Delimiter to Convert Military Time to Civilian Time

  • Thread starter Thread starter major_maths
  • Start date Start date
AI Thread Summary
The discussion focuses on converting military time to civilian time in a Java program, specifically addressing issues with changing the delimiter to ":". The user is entering time in the format "14:00" but encounters problems when trying to read the input correctly. Suggestions include using integers instead of strings and employing nextInt, but the user experiences an InputMismatchException. It is noted that the delimiter needs to account for both ":" and whitespace, which can be resolved by using a regular expression. The conversation highlights the importance of correctly handling input formatting in programming.
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:
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

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