Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2007

Tip: Looking for answers? Try searching our database.

Is any IO format reader in Java?

Thread view: 
RC - 28 Aug 2007 16:40 GMT
in java.io.*;

In PrintWriter and PrintStream classes
they both have two format(...) methods.
So I can write similar like C/C++

printf, fprintf and sprintf in format like %5.1f.

But I can't find an IO class has format reader
similar to C/C++

scanf, fscanf and sscanf

I have some input data look like

"  0.1-10.2" (two spaces at the front).
They are two float numbers 0.1 and -10.2 (NOT 0.1 minus 10.2)!

The best way to read this is read them in format "%5.1f%5.1f"

If the data look like "  0.1 -10.2", then I can use StringTokenizer
Now I have to read 5 characters a time!
a24900@googlemail.com - 28 Aug 2007 20:24 GMT
> in java.io.*;
>
[quoted text clipped - 15 lines]
>
> The best way to read this is read them in format "%5.1f%5.1f"

Fixed field-width with no separating whitespace? That's ugly to do in
Java. At least one guaranteed whitespace or any other separator in
between and you could use Scanner. But without a separator you either
need to look for a third-party library, try to make sense out of
DecimalFormat.parse() and accompanying pattern junk, or do some good
old coding, like it follows.

int fieldWidths[] = { 5, 5 };
BuffredReader in = ...

...

String line;
while((line = in.readLine()) != null) {
  int start = 0;
  for(int width: fieldWiths) {
     int end = start + width;
     String field = line.substring(start, end);
     if(field.length() != width) {
        // ups, input to short
     }
     double value = Double.parseDouble(field);
     // do something with the value

     start = end;
  }
}
Lew - 28 Aug 2007 21:53 GMT
>> in java.io.*;
>>
[quoted text clipped - 18 lines]
> Fixed field-width with no separating whitespace? That's ugly to do in
> Java. At least one guaranteed whitespace or any other separator in

Shouldn't be too bad with java.nio Buffers.

Signature

Lew

Hunter Gratzner - 29 Aug 2007 07:22 GMT
> Shouldn't be too bad with java.nio Buffers.

I don't think so. But if you have some code fragments to show
otherwise please post them.
Roedy Green - 28 Aug 2007 23:35 GMT
>printf, fprintf and sprintf in format like %5.1f.

see http://mindprod.com/jgloss/printf.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 29 Aug 2007 02:57 GMT
On Tue, 28 Aug 2007 22:35:16 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>>printf, fprintf and sprintf in format like %5.1f.
>
>see http://mindprod.com/jgloss/printf.html

To read, I think you are stuck extracting strings and feed them to
custom DecimalFormats.

If you can change the format of the data to CSV, there are a ton of
options. See http://mindprod.com/jgloss/csv.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.