- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Newbie: Java to Scala translation problems
Sat, 2009-01-31, 15:39
Hello,
as a learning exercise, I have been trying to translate the following Java code to Scala:
----------------------------------------- Java code ----------------------
import java.io.*; import java.util.*; public class DoProcessBuilder { public static void main(String args[]) throws IOException {
if (args.length <= 0) { System.err.println("Need command to run"); System.exit(-1); }
Process process = new ProcessBuilder(args).start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) { System.out.println(line); }
} }
Source: http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html
---------------------------------------------------------------------------------
------------------- Scala code ---------------------------------------------
package scalaproject1
import java.io._import java.util._
object ScalaProject1 {
for (line <- br.readLine()) println(line)
}
---------------------------------------------------------------------------------
Whereas the Java compiler tolerates "args" as parameter to ProcessBuilder,which normally requires a List<E>, scalac doesn't. That's why I had to create an ArrayList called "liste" and pass it to ProcessBuilder. Is therea better way to convert args to a List?
Whereas in the original code br.readLine() returns a String, inthe Scala code above it returns a Char. Why is that?
If I pass "ls *.txt" as arguments to the ScalaProjects1 object, and there areno text files, I get a null pointer exception. In the for loop above, whatshould I do to make sure line is not null? I have triedif (line != null) println(line)but I still get a null pointer exception
Many thanks.
pr
as a learning exercise, I have been trying to translate the following Java code to Scala:
----------------------------------------- Java code ----------------------
import java.io.*; import java.util.*; public class DoProcessBuilder { public static void main(String args[]) throws IOException {
if (args.length <= 0) { System.err.println("Need command to run"); System.exit(-1); }
Process process = new ProcessBuilder(args).start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) { System.out.println(line); }
} }
Source: http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html
---------------------------------------------------------------------------------
------------------- Scala code ---------------------------------------------
package scalaproject1
import java.io._import java.util._
object ScalaProject1 {
for (line <- br.readLine()) println(line)
}
---------------------------------------------------------------------------------
Whereas the Java compiler tolerates "args" as parameter to ProcessBuilder,which normally requires a List<E>, scalac doesn't. That's why I had to create an ArrayList called "liste" and pass it to ProcessBuilder. Is therea better way to convert args to a List?
Whereas in the original code br.readLine() returns a String, inthe Scala code above it returns a Char. Why is that?
If I pass "ls *.txt" as arguments to the ScalaProjects1 object, and there areno text files, I get a null pointer exception. In the for loop above, whatshould I do to make sure line is not null? I have triedif (line != null) println(line)but I still get a null pointer exception
Many thanks.
pr
Sun, 2009-02-01, 01:37
#2
Re: Newbie: Java to Scala translation problems
If you want to iterate over the lines you could use scala.io.Source:
import scala.io.Source
val source = Source.fromInputStream(process.getInputStream)
for (line <- source.getLines) print(line)
An alternative to using "for" is to use "foreach":
source.getLines foreach print
Some might think of it as a train wreck, but then you could have:
Source.fromInputStream(process.getInputStream).getLines foreach print
On Sat, Jan 31, 2009 at 03:38:34PM +0100, Philippe de Rochambeau wrote:
> Whereas the Java compiler tolerates "args" as parameter to ProcessBuilder, which normally requires a List,
> scalac doesn't. That's why I had to create an ArrayList called "liste" and pass it to ProcessBuilder. Is there a
> better way to convert args to a List?
You don't need a List, you just need args to be passed varargs. This is done by appending : _* to the argument.
> Whereas in the original code br.readLine() returns a String, in
> the Scala code above it returns a Char. Why is that?
Because for (x <- xs) means to iterate over the elements of the sequence xs -- which scala is interpreting as a
sequence of characters.
> If I pass "ls *.txt" as arguments to the ScalaProjects1 object, and there are no text files, I get a null pointer
> exception.
In that case you're iterating over a null sequence.
Here it is rewritten.
object ScalaProject1 {
def main(args : Array[String]) : Unit = {
if (args.length <= 0) {
System.err println("Need command to run")
System exit(-1)
}
val process = new ProcessBuilder(args: _*) start
val is = process.getInputStream
val isr = new InputStreamReader(is)
val br = new BufferedReader(isr)
printf("Output of running %s is:", args toString)
def printLines(b: BufferedReader): Unit = b.readLine match {
case null =>
case s => println(s) ; printLines(b)
}
printLines(br)
}
}