J2SE 1.5 Formatted Input Question

Discussion in 'Java' started by Matt, Oct 18, 2004.

  1. Matt

    Matt Guest

    If we have input Joe 25 in command line, then we are able to parse it
    by following code:

    Scanner s = new Scanner(System.in);
    String name = s.next();
    String age = s.nextInt();
    s.close();


    However, what if the input is 10:25:33? Are we able to parse it like
    sscanf in C? Like the following C code?

    char* buf = "10:25:33";
    sscanf(buf, "%d:%d:%d", &h, &m, &s);

    Please advise. thanks!!
     
    Matt, Oct 18, 2004
    #1
    1. Advertising

  2. Matt

    Real Gagnon Guest

    > However, what if the input is 10:25:33? Are we able to parse it like
    > sscanf in C? Like the following C code?
    >
    > char* buf = "10:25:33";
    > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    >
    > Please advise. thanks!!
    >


    import java.util.*;

    class TestScanner {
    public static void main(String args[]) {
    String input = "10:11:12";
    Scanner sc = new Scanner(input).useDelimiter(":");
    while (sc.hasNextLong()) {
    int i = sc.nextInt();
    System.out.println(i);
    }
    }
    }


    Bye.

    --
    Real Gagnon from Quebec, Canada
    * Looking for Java or PB snippets ? Visit Real's How-to
    * http://www.rgagnon.com/howto.html
     
    Real Gagnon, Oct 19, 2004
    #2
    1. Advertising

  3. Matt

    Real Gagnon Guest

    Real Gagnon <realgagnon_@_yahooSpamIsBadSstripunderscore.com> wrote in
    news:Xns9586D890A817Crealhowtowwwrgagnonc@140.99.99.130:

    > while (sc.hasNextLong()) {


    oups, replace with sc.hasNextInt()

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Looking for Java or PB snippets ? Visit Real's How-to
    * http://www.rgagnon.com/howto.html
     
    Real Gagnon, Oct 19, 2004
    #3
  4. > However, what if the input is 10:25:33? Are we able to parse it like
    > sscanf in C? Like the following C code?
    >
    > char* buf = "10:25:33";
    > sscanf(buf, "%d:%d:%d", &h, &m, &s);
    >
    > Please advise. thanks!!


    I guess you will work with date/time data, why don't use SimpleDateFormat?
    (In this example it is started from Jan 01 1970)

    import java.text.SimpleDateFormat;
    import java.util.*;

    public class DateFormatTest {

    public static void main(String[] cmdArgs) throws Exception {
    String input = "10:25:33";

    SimpleDateFormat sdfInput = new SimpleDateFormat("hh:mm:ss");
    Date date = sdfInput.parse(input);
    System.out.println(date);
    }
    }

    $ java DateFormatTest
    Thu Jan 01 10:25:33 MET 1970

    -
    Eric
     
    Eric Reitmaier, Oct 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martijn Mulder

    J2SE 1.4.2 or J2SE 5.0?

    Martijn Mulder, Jun 27, 2005, in forum: Java
    Replies:
    15
    Views:
    1,848
    Tor Iver Wilhelmsen
    Jun 28, 2005
  2. TheDD
    Replies:
    4
    Views:
    517
    John Harrison
    May 13, 2004
  3. TheDD
    Replies:
    7
    Views:
    385
    TheDD
    May 15, 2004
  4. Replies:
    0
    Views:
    523
  5. dkk

    formatted input/output question

    dkk, Apr 10, 2006, in forum: C Programming
    Replies:
    2
    Views:
    317
    Herbert Rosenau
    Apr 12, 2006
Loading...

Share This Page