Scala, how to read more than one integer in one line and get them one variable each?

here is my code:

object theater extends App { val m = readInt val n = readInt val a = readInt val c1 = m/a + (if(m%a == 0) 0 else 1) val c2 = n/a + (if(n%a == 0) 0 else 1) print(c1 + c2) } 

But input format: 3 integers in one line. But for 3 integers in one line, scala will consider this as a string. How can I read this line and get 3 values ​​in three separated variables?

+8
scala io integer
source share
2 answers

You can use the following code, which will read the line and use the first three characters separated by spaces as input. (Expects, for example, "1 2 3" as an input on one line)

 val Array(m,n,d) = readLine.split(" ").map(_.toInt) 
+22
source share

You can use Java.util.Scanner in scala programs. It supports scanner functions available in java

 import java.util.Scanner; object Addition{ def main(args: Array[String]){ var scanner = new Scanner(System.in); //defining scanner object println("Enter two numbers : "); var a = scanner.nextInt(); //reading space separated input var b = scanner.nextInt(); println("The result is : "+(a+b)); } } 
0
source share