• Post Reply Bookmark Topic Watch Topic
  • New Topic

IO the Tiger?

 
Barry Burd
Author
Ranch Hand
Posts: 118
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know where I can look for information on I/O changes in the upcoming Java 1.5 release? In particular, will there be something akin to the C-language scanf? One way or another, will there be a way to input numeric values from the keyboard -- a way that involves issuing only one statement the way scanf does?
Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In particular, will there be something akin to the C-language scanf?

Something that's prone to buffer overruns, not typesafe, and causes untold number of newbies to tear their hair out for days on end? No. Something that lets you read a number from System.in with one line? Sure, how about

This is ASCII only (also like scanf!) At the expense of a bit more complexity, it could handle international character sets as well if you used an InputStreamReader to read characters.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really hoping for non-blocking SSL/TLS support. It's supposed to be there, if I recall correctly, but I haven't heard any mention of the issue for months.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hola, Barry! Almost missed this as I was offline for a couple weeks. . Glad to see you're still around.
First off: "IO the Tiger?" Ouch. That was bad.
The best online source I could find was the notes from Josh & Neal's JavaOne presentation on Tiger, here. (You were there, weren't you?) Section V on varargs shows an example of how the new syntax will work with MessageFormat's format() method, which is analogous to C's printf. You can go to JavaLobby and follow their links to the JDK 1.5 alpha release which is currently available to JavaLobby members. (Free registration, no big deal.) Download the alpha an you'll see that there is indeed a new method in java.text.MessageFormat:

I guess I should note that the second ellipsis is inserted by me to indicate I'm omitting the source code - but the first one, in the signature, is actual source - that's the new vararg signifier in action. Guess I need to find a new way to indicate I'm omitting code without confusing people too much...
As for scanf() - well MessageFormat already had a parse() which is roughly analogous. It returns an Object[] of the parse results. There doesn't seem to be a way to assign all the results to different variables in one line the way you can with scanf, but unboxing (with a little prodding from a cast operator) does make things a little nicer:

At least I think this is how it works.
This seems fairly good if you're just dealing with numbers, dates, and enumerable choices. But I don't see a way in all this to parse something with an arbitrary string in it. I guess that's because that's a more complex problem, and you should probably use regex instead. Or maybe I'm overlooking some alternative; I haven't really used MessageFormat before. Please let us know if you find something better.
I suspect we may never see a one-line equivalent to scanf() in Java, since even now that the varargs issue has been addressed, there's no apparent way to assign multiple results in one line. In C's scanf we'd pass pointers to our output parameters to let the funtion assign to them - in Java this is still verboten of course. Though I suppose they could make a new syntax to allow this, similar to Python's treatment of tuples:

But I don't see a pressing need for something like this, especially since Java requires the variables to be declared some how. You either declare them previously in a separate line, or you do something like

and I think this would just be encouraging people to cram too much into one line anyway. (If they aren't already.)
Hum, not being able to do everything in one line doesn't really bother me here. But the (apparent?) inability to include argitrary strings in a formatted message seems an annoying limitation. I can accept that it might make things too complicated for parsing, but for formatting it doesn't seem like too much to ask to be able to doe somethign like this, does it?

But no, we don't have a "string" format in MessageFormat, can't do that...
Well, at least now that varargs are available the door is open for others to provide more satisfactory replacements (assuming Sun doesn't have something else in the pipeleine I'm unaware of). Cay Horstmann provided Format.java long ago, but it was somewhat limited by the lack of varargs. Maybe someone will feel like building on that...
[ January 09, 2004: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!