Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > J2SE 1.5 Formatted Input Question

Reply
Thread Tools

J2SE 1.5 Formatted Input Question

 
 
Matt
Guest
Posts: n/a
 
      10-18-2004
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!!
 
Reply With Quote
 
 
 
 
Real Gagnon
Guest
Posts: n/a
 
      10-19-2004
> 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
 
Reply With Quote
 
 
 
 
Real Gagnon
Guest
Posts: n/a
 
      10-19-2004
Real Gagnon <realgagnon_@_yahooSpamIsBadSstripunderscore.com > wrote in
news:Xns9586D890A817Crealhowtowwwrgagnonc@140.99.9 9.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
 
Reply With Quote
 
Eric Reitmaier
Guest
Posts: n/a
 
      10-19-2004
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Migration from J2SE 1.4 to J2SE 5: cannot validate XML file nstanevski@gmail.com Java 0 06-18-2006 12:25 PM
formatted input/output question dkk C Programming 2 04-12-2006 05:27 PM
J2SE 1.4.2 or J2SE 5.0? Martijn Mulder Java 15 06-28-2005 07:23 PM
Formatted input interpretation TheDD C++ 7 05-15-2004 10:19 PM
From unformatted to formatted input TheDD C++ 4 05-13-2004 09:51 AM



Advertisments