views:

42

answers:

1

Hi Guys,

Yesterday i posted a question about a problem i had concerning inputstream reading and i was helped.

I find myself in similar situation but this time i know that I am doing the right thing but yet it is not working for me.

I am reading from an inputstream but i get different value. No matter how i change the data i send i get the same string ("toForklift-42") as the value. At first i prefix the value i send with "toForklift-" but i have changed that string to different strings yet i get the same string in the bracket. i Even changed the number 42 to a different number but yet when i run the program, i get the same string in the console. Below is what am sending : as

Two classes for sending and receiving.

Am using the leJOS NXJ NXTConnector to make the connection and open the streams.

// sender class

class PanButton implements Runnable {

    DataInputStream dis;
    DataOutputStream dos;
    TouchSensor touch = new TouchSensor(SensorPort.S4);





    PanicButtonCrossing(DataInputStream is, DataOutputStream os) {
    dos = os;
    dis = is;
    }

    public void run() {

        while (!touch.isPressed())  {}

            // If you get a message: KILL EVERYTHING    
            Motor.A.stop();
            Motor.B.stop();
            Motor.C.stop();

            try { // send 42
                int value = 42;

                dos.writeChars("ggggggggg" + 455 + "\n"); 
                dos.flush();
                Sound.systemSound(true, 3);
            } catch (IOException ioe) {
                LCD.drawString("Write Exception", 0, 0);
            }

            System.exit(1);


}






// Reader classs



public class InputReaderCrossing  implements Runnable{


      private DataInputStream dataIn;
      private DataOutputStream dataOut;

public InputReaderCrossing(DataInputStream dataIn, DataOutputStream dataOut) {
            this.dataIn = dataIn;
            this.dataOut = dataOut;
            this.sensor = sensor;
            this.readLock = new Object();

        }


public void run(){

while(true){

String dataFromCrossing1 = readLineFromCrossing();
System.out.println("CROSSING VALUE: " + dataFromCrossing1  + "  :VALUEEEEE");
}

}

private String readLineFromCrossing() throws IOException{ 
              StringBuffer sb = new StringBuffer(); 

              synchronized(readLock){ 
                 while(true) { 
                    char c = this.dataIn.readChar(); 
                    if (c == '\n') break; 
                    sb.append(c);
                 }
                 return sb.toString(); 
              } 
           }


}

I need your help please. i have spent 6 hours but can't find the the reason. I don't understand no matter what i send i get "toForklift-42".

At first i tried to send the 42 with the writeInt() methods but then on the reader class side i use readInt() but i get somethings like:

745687894
459877455
456987456

So i changed to the string to find out why and lo and behold i get that string no matter what i send. it is like that string is fixed in the input stream and nothing is sent. I don't know what is happening.

Need help

A: 

This is a very confusing question. We don't really know which process is writing which data, and how it's being transmitted, and which process is picking it up. There's some relevant code to see here, but I didn't see enough to tell the story. How did dataFromCrossing1 get its value?

I think it would do good to organize your problem in such a way that you can ask an outsider a question about it. Possibly in the process of doing so you could stumble on the solution yourself.

In principle, if you are changing string constants in your program yet you continue to see the same output, then what's happening is that

  • The original program is still running; or
  • all versions since the original have had errors, so the class files you once compiled successfully are running again and again; or
  • you're not really running the program you think you're running.
Carl Smotricz
It gets its value from this method : readLineFromCrossing()
Edy Moore
Yes, bu in which method? The Line of code that describes the assignment is not in any method or initializer. You copy/pasted your code to make it unreadable.
f1sh