CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2013
    Posts
    20

    one entry line of code, Java

    Hello everyone, I am new to Java and need some help. the question is as follows.....

    Create a modified version of this project as follows:

    Allow the user to enter the hours, minutes, and seconds in one prompt instead of three separate prompts. That is, the user would be allowed to represent 1 hour 28 minutes and 42 seconds by entering:

    1:28:42

    This will require you to use the colon ( as a delimeter in the scanned input.

    I have the following code which works well, but I need to adjust it to where I only have one manual entry rather than three.

    import java.util.Scanner;
    import java.io.*;

    public class userinput {

    public static void main(String[] args)
    {
    Scanner time = new Scanner(System.in);
    String hour;
    String min;
    String sec;
    String cotime;


    hour = time.next();
    min = time.next();
    sec = time.next();

    cotime = (hour + ":" + min + ":" + sec);

    System.out.print(cotime);

    }
    }

  2. #2
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: one entry line of code, Java

    Your "user name generator" post from 20th January contains an example of the code you need to do this ... i.e. String.split(), this time using the colon as the delimiter.
    Code:
            String timeString = time.next();
            String[] splits = timeString.split(":");
            String hour = splits[0];
            String min = splits[1];
            String sec = splits[2];
    However, in reality this code is not resilient as it assumes that the input data is good and will split into 3 sub-strings. It will fail if the user does not input good data (on purpose?) - for example if the user inputs 1.28.45 instead of 1:28:45. Or if the user inputs ABCdEF ?
    What you need is to validate the input before you use it. Fortunately the String class contains a method called "matches" which allows you to compare the String to what's known as a "regular expression" - which you can think of as a "pattern" to describe the input. So the code should be amended to check that the input matches a pattern of start - one or more digits - colon - one or more digits - colon - one or more digits - end. Anything which does not match this pattern should be considered invalid.
    Code:
            if ( ! (timeString.matches("^\\d+:\\d+:\\d+$"))) {
                System.err.println("Usage: userinput hh:mm:ss");
            }
            else {
                String[] splits = timeString.split(":");
                String hour = splits[0];
                ...etc ...
    OK, after this point you know your input is all digits with colons separating the hour, minute and second fields. What you don't yet know is that the numbers themselves are valid ... e.g 1:90:45 - should not be valid because there are not 90 minutes in an hour (should be 2:30:45). So subsequent code should really check the validity of each field and return error if it is not valid.
    Welcome to the world of real programming - where a significant portion of your time is spent guarding against poor quality input data!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured