0

I have a piece of code, that receives values from a sensor (Via serialport using rxtx) and displays it. Strangely, the following code

int value = in.read();
System.out.print((char) value);

Outputs the desired value as:

RXTX Warning:  Removing stale lock file. /var/lock/LK.005.018.009
20
27
29
26
21

But when I change the above code as following:

int value = in.read();
System.out.print("The value is"+(char) value);

The output becomes:

RXTX Warning:  Removing stale lock file. /var/lock/LK.005.018.009
The value is2The value is6The value is
The value is2The value is2The value is

As it can be seen, the integer splits. For quite a while, I am unable to figure it out?

Is there a way where I can save the console value into an integer, as I would be using this value in the future.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Spaniard89
  • 2,359
  • 4
  • 34
  • 55

5 Answers5

2

As it can be seen, the integer splits. For quite a while, I am unable to figure it out?

You are not reading integers, you are reading bytes which have characters encoded as ?ASCII?

Is there a way where I can save the console value into an integer, as I would be using this value in the future?

The simplest way is to use a Scanner

Scanner scanner = new Scanner(in);
while (scanner.hasNextInt()) {
   // read bytes up the next whitespace, parse as a int
   int n = scanner.nextInt();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I tried using the scanner. I tried incorporating the above code of yours as follow: `while (scanner.hasNextInt()) { // read bytes up the next whitespace, parse as a int int n = scanner.nextInt(); System.out.println("integer value is"+n); }` And the output I get is :`2integer value is1 1integer value is9` – Spaniard89 Jan 08 '13 at 10:18
  • 1
    Thanks a lot, it was helpful. I tried some modification in your code. And got it working. – Spaniard89 Jan 08 '13 at 10:56
  • Why do you get a number before the word "integer"? – Peter Lawrey Jan 08 '13 at 10:56
  • I have programmed my sensor in a way that, it would send the value every 10 sec. And I think the sensor tramsits the value one by one. In the above output 1 is transmitted first and 9 again. And it is displayed as 19 on the output screen. So in the above code, scanner is called immediately once a number is transmitted. I tried calling the scanner part every 5sec. So now it works like charm :) – Spaniard89 Jan 08 '13 at 11:02
0

Don't cast it to char. Just print

System.out.print("The value is " + value);
Zoltán
  • 21,321
  • 14
  • 93
  • 134
  • 1
    I tried it, but when I do that, I get some random undesired value. I have programmed my sensor in such a way that, every 10 sec it would transmit the light value. But when I just print it out as it is, For every 10 sec I get more than 4-5 values like this : `The value is50The value is51The value is10The value is50` – Spaniard89 Jan 08 '13 at 10:14
0

In second example you are converting int to char, but when you concatenate it to string you don't see anything because these are non printable characters.

partlov
  • 13,789
  • 6
  • 63
  • 82
0

Your code converted the value number into a character, using Java's UCS2 representation. You may need it only if you want to treat the value as a character data (e.g. String).

If you need it only as an integer value (e.g. for printing), you don't need to convert.

gaborsch
  • 15,408
  • 6
  • 37
  • 48
  • I tried not casting it into char, but I don't get the appropriate output. – Spaniard89 Jan 08 '13 at 10:21
  • OK, I can see from the accepted answer that your main problem was how to READ data from the input channel. This way the title for the topic is a bit misleading. – gaborsch Jan 09 '13 at 08:58
0

please check this snippet by adopting to your code

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
   String  line = br.readLine();
   String[] words = line.split("[ ]",0); //white space delimiter
   for(int i = 0; i < words.length; i++) {
   System.out.print("The value is " +words[i]);
   System.out.print("\n");
Chethan R
  • 11
  • 2