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 / General / March 2006

Tip: Looking for answers? Try searching our database.

What's wrong with my use of DataInputStream.readUTF() ??

Thread view: 
rhimbo - 23 Mar 2006 23:32 GMT
Hi folks,

I'm writing a simple program to prompt the user at the console and read
the line of data items entered. The data will consist of
space-separated items of type int and String like this:

1233 "some text" "more text"

I want to read a line of data at a time and then extract the elements
with the DataInputStream methods readInt(), readUTF(), etc.

My program seems to hang waiting on the DataInputStream.readUTF() call.
I can call every other DataInputStream method just fine, such as
readInt(), readLong(), etc.

Actually, when I change the program to read one item at a time I get
the same behavior. I can read everything fine except the strings. The
readUTF() call hangs.

Here is the basic loop of the program.

 ...
 int uid = 0;
 boolean online = false;
 String username = null;
 String email = null;
 String message = null;

 public void run()
 {
   DataInput dataInput = new DataInputStream(System.in);

   while (true)
     {
    System.out.println("Enter record to insert in database...");
    System.out.println("<uid> <username> <email> <online ?> <msg>...");
    System.out.print("? ");

    try
     {
       uid = dataInput.readInt();
        username = dataInput.readUTF();
         email = dataInput.readUTF();
        online = dataInput.readBoolean();
        message = dataInput.readUTF();
     }
    catch (EOFException eof)
     {
       // Reached end of input.
       System.out.println(eof.getMessage());
       break;
     }
    catch (UTFDataFormatException dfe)
     {
       System.out.println(dfe.getMessage());
       break;
     }
    catch (IOException ioe)
     {
       System.out.println(ioe.getMessage());
       break;
     }

    outputData(output);
     }
 }
Chris Uppal - 24 Mar 2006 13:14 GMT
> 1233 "some text" "more text"
>
> I want to read a line of data at a time and then extract the elements
> with the DataInputStream methods readInt(), readUTF(), etc.

DataInputStream is the wrong tool for this job -- it reads a /binary/
representation of integers (etc) from the underlying binary stream.

You should wrap a java.io.InputStreamReader around System.in, and go from
there.

   -- chris
Roedy Green - 24 Mar 2006 19:52 GMT
On Fri, 24 Mar 2006 12:14:43 -0000, "Chris Uppal"
<chris.uppal@metagnostic.REMOVE-THIS.org> wrote, quoted or indirectly
quoted someone who said :

>> 1233 "some text" "more text"
>>
[quoted text clipped - 6 lines]
>You should wrap a java.io.InputStreamReader around System.in, and go from
>there.

for sample code see http://mindprod.com/applets/fileio.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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.