0

I'm trying to make the program go through a predetermined string and read each character 1 by 1 in a manner similar to what I have posted. The IDE tells me that I cannot do "cs >= 1" because ">=" is not part of "(Char => Boolean) => Int".

def move(s: String) {
  var chemov = s.take(1)
  var cs = s.count(_)
  while (cs >= 1){
    ad()
    s.drop(1)
  }
}
2
  • 1
    note that s does not modify by s.drop(1). That function returns the string without the first character. Also cs, never updates, so even if cs was an integer, the loop would never terminate. Commented May 6, 2020 at 22:19
  • What is your intention with the variable chemov? It contains the first character of s, but since it is never used, and s.take(1) does not modify s, the operation has no effect. Commented May 6, 2020 at 22:21

1 Answer 1

2

s.count() does not give you the length of the s, but it gives you the number of occurences that the predicate matches. By just supplying an underscore, cs is not an integer, but a function. That is why you get your error. You can get the size with s.length

If you want to use count, then you have to supply a function:

var cs = s.count(_ => true)

Alternatively, you can just iterate over the string:

s.foreach( c => {
    ad()
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.