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.