0

My code is as follows:

myBuffer = myInput.read()
 if (!myBuffer.!=(-1)) {
    return -1
 }

myBuffer is declared as a var int at the top of the class and I am trying to check when it equals -1 so that I can return -1. When I tried doing it the Java way, like so:

if ( (myBuffer = myInput.read()) == -1) {
     return -1;
}

I got an warning saying that a test for equality with Unit will always give false, which was verified when I ran it and it never exited the loop that this code was a part of. This feels like it should be trivial but I haven't been able to find a solution to this. Any ideas?

covfefe
  • 2,485
  • 8
  • 47
  • 77
  • 1
    The assignment `myBuffer = myInput.read()` returns a `Unit`, and not the value of `myInput.read()`. What are you trying to accomplish overall? – Michael Zajac Apr 23 '16 at 03:37
  • I'm trying to read from a file (myInput is an InputStream) and stop reading at the end of file (indicated by the -1). I wrote this code in Java and am trying to port it to Scala – covfefe Apr 23 '16 at 14:25

1 Answers1

0

using var is not recommended in Scala as you may know.

try scala.io.StdIn.readLine()

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82