Two Laptop Bag*
The moose likes Beginning Java and the fly likes Unexpected leading characters in buffered reader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unexpected leading characters in buffered reader" Watch "Unexpected leading characters in buffered reader" New topic
Author

Unexpected leading characters in buffered reader

John Hagan
Greenhorn

Joined: Jan 04, 2013
Posts: 2
Hi,
I am an amateur programmer programming for the Lego NXT robot using Lejos. I say this because some of the methods/classes I am using may be unique to Lejos. I have two programs interacting. The first is on a W7 PC. It runs from the command line in a console. It is called PC_BT_Connect. The second program (BTReceiveTest) is a blue tooth listener running on the robot, which also runs java. I begin by starting the listnener on the robot. Then I start the command line program which seeks out and connects successfully. I enter characters a line at a time and send them via the bluetooth connector. The robot receives them and prints the line to its screen. Then it turns the string around and sends it back to the PC where it is output to the console. This all works fine, but I am picking up a random first character that gets posted to the PC console on the return message. Can you help me find out where this is coming from?



This is the robot listener:



This is the output:



--John
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

Why are you constantly opening a new DataInputStream on btc?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7553
    
  18

John Hagan wrote:...but I am picking up a random first character that gets posted to the PC console on the return message. Can you help me find out where this is coming from?

I honestly don't know, but I wonder if it might be a byte order mark.

I've never used Lego, but from a quick glance at the NXTComm docs (I assume I'm looking at the right one), it doesn't appear to require a DataInputStream; and since you're only sending text, I'm wondering why you chose it.
If it were me, I think I'd want to use a BufferedReader, possibly with a specific Charset like UTF-8. That way, you don't have to write your own readline() method, because BufferedReader already has one, and BOMs (if sent/received) would be handled internally.

However, as I say, I've never used Lego, so there may be other things going on that I don't understand.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
John Hagan
Greenhorn

Joined: Jan 04, 2013
Posts: 2
Stephan, thanks for pointing out the fact that I had the streams defined in the wrong place. Winston, I had to go back and relearn input and output streams and stream readers. The leading character did not disappear until I eliminated all of the Data input/output streams. Thanks for helping me clean up my code. I post the updated code and output below for your review and comments.

the code from the W7 PC side:


the code for the NXT robot side:

This is the output on the windows console:


Thanks again. --John
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Unexpected leading characters in buffered reader
 
Similar Threads
email code in j2me application
Using DataInputStream (Handling EOFException)
DataInputStream Help needed while creating file
Socket program for linux
Exception in thread "main" java.io.EOFException