Strange error comparing strings

24 views
Skip to first unread message

Sankha Narayan Guria

unread,
May 31, 2012, 2:51:56 AM5/31/12
to scala...@googlegroups.com
Hi,
    I have the following code segment

var reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt")))
var line:String = null
while(!((line = reader readLine) == "")) {
  // my code here
}

In Eclipse while editing the code the editor is showing an error that comparing Unit and java.lang.String  using ' == ' will always yield false. Why is it so? I am comparing 2 String objects only. How come Unit is coming in?

Thanks,
Sankha Narayan Guria

Rex Kerr

unread,
May 31, 2012, 3:06:19 AM5/31/12
to Sankha Narayan Guria, scala...@googlegroups.com
Assignments return unit, not the value that was assigned.

Change that to
  { line = reader readLine; line } == ""
and it should compile (and possibly even do what you want).

  --Rex

Ken Scambler

unread,
May 31, 2012, 3:05:22 AM5/31/12
to Sankha Narayan Guria, scala...@googlegroups.com
Assignment always returns Unit; you can't use this idiom in Scala.  Most style guides in Java, C++, etc would discourage assignment within if/while conditions anyway.
Reply all
Reply to author
Forward
0 new messages