使用implicate类型种姓会改变变量的类型吗?

时间:2012-04-05 01:41:01

标签: scala type-conversion

我从一段代码中收到错误。我只会显示一行代码,至少我相信是从错误报告中引出它的行。它是:

b = temp(temp.length-1).toInt; //temp is an ArrayBuffer[String]

错误是:

For input string: "z"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:231)
at scala.collection.immutable.StringOps.toInt(StringOps.scala:31)
at Driver$.stringParse$1(Driver.scala:59)
at Driver$.main(Driver.scala:86)
at Driver.main(Driver.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

据我所知,这引起了一个问题。由于它是不可变的,我知道它无法改变。但我不确定。我的基础是

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

一旦我做了上面的代码,它会改变整个对象吗? Temp是一个ArrayBuffer [String]。所以我试图访问一个数字的字符串表示,并转换它。但是这样做,这会改变它是什么,让我不做任何事情吗?

如果您认为我的所有代码都会有所帮助,请让我知道编辑它,但它很多,我不想惹恼任何人。我感谢任何能帮助我理解这一点的人!

*编辑:我的代码(仅在这里帮我弄清楚我的错误,但没有必要去看。我只是看不出它给我这个错误的地方)。

我的代码的意思是解析顶部的其中一个字符串。它将一个字符串放在一起,然后读取其他两个符号。它解析str很好,但是当它在str2中读取“z”而在str3中读取“y”时会发现问题。可以看出,问题在于递归之后的第二个字符串。同样重要的是要注意字符串必须是那种形式。因此它只能被解析为“(和x(和y z))”,但不能以任何其他方式进行解析,使其更方便。

val str = "(and x y)";
val str2 = "(and x (and y z))"; //case with expression on th right side
val str3 = "(and (and x y) z)"; //case with expression ont he left side

var i = 0; //just counter used to loop through the finished parsed array to make a list
//var position = 0; //this is used for when passing it in the parser to start off at zero
var hold = new ArrayBuffer[String]();//finished array should be here

def stringParse ( exp: String, expreshHolder: ArrayBuffer[String] ): ArrayBuffer[String] = { //takes two arguments, string, arraybuffer
    var b = 0; //position of where in the expression String I am currently in
    var temp = expreshHolder; //holder of expressions without parens
    var arrayCounter = 0;
    if(temp.length == 0) 
        b = 0; 
        else {
            b = temp(temp.length-1).toInt;
            temp.remove(temp.length-1); 
            arrayCounter = temp.length;
        } //this sets the position of wherever the string was read last plus removes that check from the end of the ArrayBuffer
     //just counts to make sure an empty spot in the array is there to put in the strings

    if(exp(b) == '(') {
        b = b + 1;

        while(exp(b) == ' '){b = b + 1;} //point of this is to just skip any spaces between paren and start of expression type
        if(exp(b) == 'a') {
            //first create the 'and', 'or', 'not' expression types to figure out 
            temp += exp(b).toString;
            b = b+1; 
            temp(arrayCounter) = temp(arrayCounter) + exp(b).toString; //concatenates the second letter
            b = b+1; 
            temp(arrayCounter) = temp(arrayCounter) + exp(b).toString; //concatenates the last letter for the expression type
            //arrayCounter+=1;
            //this part now takes the symbols and puts them in an array
            b+=1;

            while(exp(b) == ' ') {b+=1;} //just skips any spaces until it reaches the FIRST symbol
            if(exp(b) == '(') { 
                temp += b.toString; 
                temp = stringParse(exp, temp); 
                b = temp(temp.length-1).toInt; 
                temp.remove(temp.length-1); 
                arrayCounter = temp.length-1 
                } else {
                    temp += exp(b).toString; 
                    arrayCounter+=1; b+=1; }

            while(exp(b) == ' ') {b+=1;} //just skips any spaces until it reaches the SECOND symbol
            if(exp(b) == '(') { 
                temp += b.toString;
                temp = stringParse(exp, temp); 
                b = temp(temp.length-1).toInt; 
                temp.remove(temp.length-1); 
                arrayCounter = temp.length-1 
                } else {
                    temp += exp(b).toString; 
                    arrayCounter+=1; 
                    b+=1; 

        } 
    temp;
    } else { var fail = new ArrayBuffer[String]; fail +="failed"; fail;}

}
hold = stringParse(str2, ho );
for(test <- hold) println(test);

3 个答案:

答案 0 :(得分:2)

temp包含哪些内容?您的代码假定它包含可以转换为String的{​​{1}},但您似乎在其中有Int String。这会产生错误:

"z"

这是scala> "z".toInt java.lang.NumberFormatException: For input string: "z" ... 可能的样子:

temp

所以你需要找出val temp = ArrayBuffer("1", "2", "z") temp(temp.length-1).toInt //java.lang.NumberFormatException: For input string: "z" String进入"z"的原因。

编辑:

因此,您要将“表达式”添加到temptemp)并添加索引(temp += exp(b).toString)。然后你假设temp += b.toString只持有索引(temp)。您需要确定b = temp(temp.length-1).toInt的用途,然后专门用于此目的。

答案 1 :(得分:1)

不,toInt不会更改对象,它会将对象作为参数并返回一个整数,使对象保持原样。

答案 2 :(得分:1)

我无法理解你的问题,因为我无法理解你的代码。 我们试着简化你的代码。

首先:你有一些带有表达式类型和操作数列表的表达式:

scala> :paste
// Entering paste mode (ctrl-D to finish)

abstract sealed class Operand
case class IdentOperand(name: String) extends Operand { override def toString(): String = name }
case class IntOperand(i: Int) extends Operand  { override def toString(): String = i.toString() }
case class ExprOperand(expr: Expression) extends Operand { override def toString(): String = expr.toString() }
case class Expression(exprType: String, operands: Seq[Operand]) {
  override def toString(): String = operands.mkString("(" + exprType + " ", " ", ")")
}

// Exiting paste mode, now interpreting.

defined class Operand
defined class IdentOperand
defined class IntOperand
defined class ExprOperand
defined class Expression

scala> Expression("and", Seq(IdentOperand("x"), IdentOperand("y")))
res0: Expression = (and x y)

scala> Expression("and", Seq(IdentOperand("x"), ExprOperand(Expression("and", Seq(IdentOperand("y"), IdentOperand("z"))))))
res1: Expression = (and x (and y z))

scala> Expression("and", Seq(ExprOperand(Expression("and", Seq(IdentOperand("x"), IdentOperand("y")))), IdentOperand("z")))
res2: Expression = (and (and x y) z)

现在我们必须将字符串解析为此类型的表达式:

scala> import scala.util.parsing.combinator._
import scala.util.parsing.combinator._

scala> object ExspessionParser extends JavaTokenParsers {
     |   override def skipWhitespace = false;
     |
     |   def parseExpr(e: String) = parseAll(expr, e)
     |
     |   def expr: Parser[Expression] = "(" ~> exprType ~ operands <~ ")" ^^ { case exprType ~ operands => Expression(exprType, operands) }
     |   def operands: Parser[Seq[Operand]] = rep(" "~>operand)
     |   def exprType: Parser[String] = "and" | "not" | "or"
     |   def operand: Parser[Operand] = variable | exprOperand
     |   def exprOperand: Parser[ExprOperand] = expr ^^ (ExprOperand( _ ))
     |   def variable: Parser[IdentOperand] = ident ^^ (IdentOperand( _ ))
     | }
defined module ExspessionParser

scala> ExspessionParser.parseExpr("(and x y)")
res3: ExspessionParser.ParseResult[Expression] = [1.10] parsed: (and x y)

scala> ExspessionParser.parseExpr("(and x (and y z))")
res4: ExspessionParser.ParseResult[Expression] = [1.18] parsed: (and x (and y z))

scala> ExspessionParser.parseExpr("(and (and x y) z)")
res5: ExspessionParser.ParseResult[Expression] = [1.18] parsed: (and (and x y) z)

现在(据我了解你的代码),我们必须用整数值替换字符串操作数(xyz)。我们将这两个方法添加到Expression类:

  def replaceOperands(ints: Seq[Int]): Expression = replaceOperandsInner(ints)._2

  private def replaceOperandsInner(ints: Seq[Int]): (Seq[Int], Expression) = {
    var remainInts = ints
    val replacedOperands = operands.collect{
      case n: IdentOperand =>
        val replacement = remainInts.head
        remainInts = remainInts.tail
        IntOperand(replacement)
      case ExprOperand(e) =>
        val (remain, replaced) = e.replaceOperandsInner(remainInts)
        remainInts = remain
        ExprOperand(replaced)
    }

    (remainInts, Expression(exprType, replacedOperands))
  }

现在我们可以这样做:

scala> ExspessionParser.parseExpr("(and (and x y) z)").get.replaceOperands(Seq(1, 2, 3))
res7: Expression = (and (and 1 2) 3)

如果你有字符串形式的整数值,那么你可以先转换它们:

scala> Seq("1", "2", "3") map { _.toInt }
res8: Seq[Int] = List(1, 2, 3)