1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Changing Delimiters

  1. Oct 16, 2011 #1
    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 (Text):

    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);
        }
    }
     
     
  2. jcsd
  3. Oct 16, 2011 #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: Oct 16, 2011
  4. Oct 16, 2011 #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.
     
  5. Oct 16, 2011 #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: Oct 16, 2011
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Changing Delimiters
  1. Phase change? (Replies: 2)

  2. Change in enthalpy (Replies: 4)

Loading...