0

我从一段代码中得到一个错误。我只会显示一行代码,至少我认为是错误报告中导致它的那一行。这是:

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(和yz))”那样解析,但不能以任何其他更方便的方式解析。

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);
4

3 回答 3

2

包含什么temp?您的代码假定它包含String可以转换为Ints 的 s,但似乎您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"会进入temp.

编辑:

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

于 2012-04-05T01:44:07.890 回答
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)

现在(据我了解您的代码)我们必须用整数值替换字符串操作数( x, y, )。z让我们将这 2 个方法添加到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)
于 2012-04-05T08:17:14.597 回答
1

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

于 2012-04-05T01:53:21.477 回答