bajro
bajro

Reputation: 1250

Scala: load different files by input

My program has a lot of different functions and one of these is the command "load". As soon as the user types as input "load" he can load in a txt file... The problem is, that my command is not only the "load" word itself, its for example "load numbers.txt" or "load data.txt"

Now i want to open these textfiles which are located on my PC but i need the name of the files WITHOUT the "load" in front of the command. How can I fetch only the name from the entire input line?

def ProgramSelector() {
var endProgram = false

while (!endProgram) {

  val userSelection = scala.io.StdIn.readLine("There is no transfer data available yet, please use the 'load' command to initialize the application!\nEnter your command or type 'help' for more information:")
  if (userSelection == "help")
    println("some help text here")
else if (userSelection == "load")

  //else if (userSelection == "3")
    //exerciseThree()
  //else if (userSelection == "4")
    //exerciseFour()
  //else if (userSelection == "5")
    //exerciseFive()
  //else if (userSelection == "6")
    //exerciseSix()
  //else if (userSelection == "7")
    //exerciseSeven()
  //else if (userSelection == "8")
    //exerciseEight()
  else if (userSelection == "exit")
    endProgram = true
  else
    println("Invalid command!")

So I have my function the ProgramSelector where I only make an if statement if the input is load...

Upvotes: 0

Views: 160

Answers (1)

marios
marios

Reputation: 8996

I tried to make this a bit more generic.

To show how this can be helpful, I also created another command that you can call as "add 1 2" and it will print the sum of adding the two integers.

If you are serious into making a CLI interactive application, I suggest you take a look here on how to make your own interactive shell on top of sbt.

val loadCommand = """load (.*)""".r
val helpCommand = """help.*""".r
val exitCommand = """exit.*""".r
val addCommand = """add\s+(\d+)\s+(\d+)""".r

val PromptMsg = "There is no transfer data available yet, please use the 'load' command to initialize the application!\nEnter your command or type 'help' for more information: "

def programSelector() {
    var endProgram = false

    val fileKeeper = new scala.collection.mutable.HashSet[String]()

    while (!endProgram) {
        val userSelection = scala.io.StdIn.readLine(PromptMsg)
        userSelection match {
            case loadCommand(file) => 
                println(s"Adding file $file")
                fileKeeper add file
                println(s"Files so far: $fileKeeper")
            case helpCommand() => 
                println("some help text here")
            case exitCommand() => 
                endProgram = true
            case addCommand(a,b) => 
                val sum = a.toInt + b.toInt
                println(s"Sum=$sum") 
            case _ => 
                println("Invalid command!")
        }   
    }
}


programSelector() 

Upvotes: 1

Related Questions