I am currently writing a program that read a string like "2 + 2". It should be split regarding the spaces and store into an Array like Array(2, +, 2). However, I am not sure how to do it within a method (since I usually work in Scala interpreter), access to the result after I split it. I know that in java, you can do String[] arr = string.split("\s+").
def eval(s: String): Double = {
var result = 0;
s.split("\\s+");
//how do i retrieve the element that was split?
// I am trying to retrieve, like the above example, the 2 and 2
// perform an addition on them so the method will return a double
result;
}
s.split("\\s+");should work in Scala!? Just assign it to a variable or whatever you wanna do and read the array elements:val arr = s.split("\\s+");