这就是我需要的;
用户输入键值和数字;例如“SK1> 34”
系统然后使用此键值来搜索名为“mapdata”的地图列表。这是“Map(String,List [Int])”
返回此值仅包含最后一位数。
然而我遇到的问题是,当系统使用键值搜索地图时,即使输入的键值在地图中,也无法找到它。
如果我将用户stackCost更改为直接输入“SK1”,那么它可以工作。这告诉我问题在于找到“stockValue”,即使我的测试显示它正在搜索SK1。
这是我目前的代码;
def addNewStock =
{
println("Enter stock as *SK1* > AMOUNT(8) and the stock level will be taken from the stocks*")
print("Enter new Stock > ")
val key = readLine()
var result = key.split(";").map(_ split ">") collect { case Array(k, v) => (k, v) } toMap
println ("Data entered into file " + result)
//String.valueOf(
var stockValue = String.valueOf(result.head._1)
var stockCost = mapdata.get(stockValue)
println("This is the stock value " + stockValue)
println("This is the stock cost " + stockCost)
}
正在查询的mapdata;
SK1, 9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1
SK2, 0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1
SK4, 2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8
SK5, 2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3
SK6, 2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9
SK7, 6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0
SK8, 2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6
SK9, 7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6
EDITS ***
var testing = result.toList
var stockValue = String.valueOf(result.head._1)
var stockCost = mapdata.get(stockValue).toList
var stockCheck = stockCost.map(_.lastOption.getOrElse(0))
var result2 = testing ::: stockCheck
答案 0 :(得分:0)
问题出在你的测试字符串中。您正在使用字符串"SK1 > 34"
进行测试。当您在此字符串上调用split(">")
时,您将获得Array("SK1 ", " 34")
。注意这两个字符串中是否有空格。这意味着当您查询mapdata
时,您使用的是字符串"SK1 "
(末尾有空格),这显然不是地图中的关键字(因为"SK1 " != "SK1"
)。此用例的更合适的测试字符串为"SK1>34"
。