Scala: load different files by input -
my program has lot of different functions , 1 of these command "load". user types input "load" can load in txt file... problem is, command not "load" word itself, example "load numbers.txt" or "load data.txt"
now want open these textfiles located on pc need name of files without "load" in front of command. how can fetch name entire input line?
def programselector() { var endprogram = false while (!endprogram) { val userselection = scala.io.stdin.readline("there no transfer data available yet, please use 'load' command initialize application!\nenter command or type 'help' more information:") if (userselection == "help") println("some 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 have function programselector make if statement if input load...
i tried make bit more generic.
to show how can helpful, created command can call "add 1 2" , print sum of adding 2 integers.
if serious making cli interactive application, suggest take here on how make 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 no transfer data available yet, please use 'load' command initialize application!\nenter command or type 'help' 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 far: $filekeeper") case helpcommand() => println("some 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()
Comments
Post a Comment