0

I wanna know how to ask user input for a string. For example:

when user input is asked "lruud" is entered

Enter letters: lruud

and I want it the return should be:

Left
right 
up
up 
down

left = l, right = r, etc.

So basically it should return the result in the order the letters are entered.

The code below is what I have tried where al() is move left, ar() is move right etc

def move(s: String) {
  if(s == "l"){
    al()
  }else if(s == "r"){
    ar()
  }else if(s == "u"){
    au()
  }else if(s == "d"){
    ad()
  }  
}
4
  • 2
    What have you tried? Show us code that doesn't work so that we can better focus our suggestions. Commented May 12, 2020 at 20:47
  • def move(s: String) { if(s == "l"){ al() }else if(s == "r"){ ar() }else if(s == "u"){ au() }else if(s == "d"){ ad() } } thats the code Commented May 12, 2020 at 20:52
  • 2
    Please edit your question and add the code there. As you can see, SO comments make for terrible code formatting. Commented May 12, 2020 at 20:54
  • 1
    sorry I am new here, havent gotten comfortable with how to use everything. But I edited my question its much clear now Commented May 12, 2020 at 21:17

2 Answers 2

2

You can read in a string from standard input using

val s = scala.io.StdIn.readLine()

Scala implicitly considers String to be a Scala collection, in particular an IndexedSeq which means you can call standard collections methods on it such as map to transform it. For example

def move(s: Char): String = {
  if (s == 'l') "left"
  else if (s == 'r') "right"
  else if (s == 'u') "up"
  else if (s == 'd') "down"
  else throw new RuntimeException("Bad input")
}

"lruud".map(move)
// res4: IndexedSeq[String] = ArraySeq(left, right, up, up, down)

Mapping over a String reads each Char in the string and passes it to the move method for transformation.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you that code works. But now what if instead of the letter "l" being equal to "left" it is equal to a function for example al(). So what I want is if the letter "l" is entered it does the function corresponding to that letter in this case if "l" then does the function al()
I tried replacing "left" with al() but gives an error saying that required string found unit
@SurajOdedra Then you need to change the return type of move from String to Unit that is def move(s: Char): Unit.
2

Here's one way to go about this.

val action :Map[Char,String] =
  Map('d'->"down",'l'->"left",'r'->"right",'u'->"up")
    .withDefault(c => s"$c?")

val moves :Seq[String] =
  io.StdIn.readLine("Enter letters: ").map(c =>action(c.toLower))

println("\ndirections:\n" + moves.mkString("  ","\n  ",""))

testing:

Enter letters: lLdRbu

directions:
  left
  left
  down
  right
  b?
  up

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.