Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
forInputString(NumberFormatException.Java:65)_Java_Bluej_Operands - Fatal编程技术网

forInputString(NumberFormatException.Java:65)

forInputString(NumberFormatException.Java:65),java,bluej,operands,Java,Bluej,Operands,每次我尝试启动代码时,都会出现相同的错误: java.lang.NumberFormatException: For input string: "x" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.lang.Integer.parseInt(Integer.ja

每次我尝试启动代码时,都会出现相同的错误:

 java.lang.NumberFormatException: For input string: "x"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:492)
 at java.lang.Integer.parseInt(Integer.java:527)
 at Variable.<init>(Variable.java:17)
 at Main.main(Main.java:4)`
java.lang.NumberFormatException:对于输入字符串:“x”
位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
位于java.lang.Integer.parseInt(Integer.java:492)
在java.lang.Integer.parseInt(Integer.java:527)处
at变量。(Variable.java:17)
Main.Main(Main.java:4)`
我也把我的代码。你能帮我理解出了什么问题吗

public class Main {

public static void main(final String[] args) { 
    final Node expression =
      new Plus(
        new Minus(
          new Plus(
            new Variable("x"),
            new Literal(2)
          ),
          new Variable("y")
        ),
        new Minus(
          new Literal(4),
          new Variable("z")
        )
      );
    // an interpreter would just evaluate the expression:
    System.out.println("Expression is: " + expression);

    // a compiler would compile the expression into a program
    System.out.println("Compiling expression...");
    final Program program = new Program();
    expression.generateCode(program);
    System.out.println("Resulting program:\n"+program);

    // and, later, that program can then be executed after the variables have been assigned
    // First assignment of variables
    VariableSpace variables = new VariableSpace();
    variables.store("x", 5);
    variables.store("y", 7);   
    variables.store("z", 1);
    System.out.println("For x = 5, y = 7 and z = 1 the program executes and returns:");
    int resultOfExecution = program.execute(variables);
    System.out.println(resultOfExecution);

    // Second assignment of variables
    variables.store("x", 11);
    variables.store("y", 3);
    variables.store("z", 2);
    System.out.println("For x = 11, y = 3, and z = 2 the program executes and returns:");
    resultOfExecution = program.execute(variables);
    System.out.println(resultOfExecution);        
}

public class Variable extends Node
{
    String variable;
    int value;
    /**
    * Constructor for objects of class Variable
    */
    public Variable(final String variable)
    {
        this.variable = variable;
        int value = Integer.parseInt(variable);
    }

    public void generateCode(final Program program) {
        program.append(new ILOAD(value));
    }

    /**
     * Return a int representing this expression
     * (e.g., new Literal(19).toint() is "19").
     */
      public String toString() {
      return "" + value;
      }
    }

import java.util.ArrayList;


/**
 * A Node in an abstract syntax tree (AST)
* for a very simple expression language.
* The language only supports the following subtypes of Nodes:
* <ul>
* <li>integer values (class Literal)
* <li>integer variables (class Variables)
* <li>the integer + operator (class Plus)
* <li>the integer - operator (class Minus)
* </ul>
* It does not support types other than integers.
*/

public class Node {

/**
 * Compile this AST into an IJVM program:
 * Append instructions to the given Program
 * such that the instructions, when executed
 * produce the same value 
 * as is produced by a call to evaluate(). 
 */
public void generateCode(Program program) {
}

/**
 * Generate a string-representation of the subtree.
 * When you implement this method in subclasses,
 * where possible use recursive calls to left.toString() and
 * to right.toString() to do this.
 */
public String toString() {
    return null;
}

}

public class ILOAD extends Instruction
{
private final int value;

public ILOAD(final int value)
{
    this.value = value;
}

public void execute(Storage storage) {
    storage.getStack().push(value);
}

/**
 * Produce a human-readable String-representation of this instruction.
 */
public String toString() {
    return "ILAOAD " + value;  
}

}

import java.util.*;

/**
* A space that stores the variables during the execution of the IJVM/Java bytecode.
*/
public class VariableSpace {

private HashMap<String, Integer> value;

public VariableSpace() {
    value = new HashMap<String, Integer>();
}

public void store(String name, int value) {
    this.value.put(name, value);   
}

public int load(String name) {
    return value.get(name);
}

}
公共类主{
公共静态void main(最终字符串[]args){
最终节点表达式=
新加号(
新负号(
新加号(
新变量(“x”),
新文字(2)
),
新变量(“y”)
),
新负号(
新文字(4),
新变量(“z”)
)
);
//解释器只会计算表达式:
System.out.println(“表达式为:”+Expression);
//编译器将把表达式编译成程序
System.out.println(“编译表达式…”);
最终程序=新程序();
表达式。生成代码(程序);
System.out.println(“结果程序:\n”+程序);
//然后,该程序可以在分配变量后执行
//变量的首次赋值
VariableSpace变量=新的VariableSpace();
变量。存储(“x”,5);
变量。存储(“y”,7);
变量。存储(“z”,1);
System.out.println(“对于x=5、y=7和z=1,程序执行并返回:”;
int resultOfExecution=program.execute(变量);
系统输出打印项次(结果执行);
//变量的二次赋值
变量。存储(“x”,11);
变量。存储(“y”,3);
变量存储(“z”,2);
System.out.println(“对于x=11,y=3,z=2,程序执行并返回:”;
resultOfExecution=program.execute(变量);
系统输出打印项次(结果执行);
}
公共类变量扩展节点
{
字符串变量;
int值;
/**
*类变量的对象的构造函数
*/
公共变量(最终字符串变量)
{
this.variable=变量;
int value=Integer.parseInt(变量);
}
公共无效生成代码(最终程序){
append(新的ILOAD(值));
}
/**
*返回表示此表达式的int
*(例如,new Literal(19)。toint()是“19”)。
*/
公共字符串toString(){
返回“”+值;
}
}
导入java.util.ArrayList;
/**
*抽象语法树(AST)中的节点
*对于一种非常简单的表达语言。
*该语言仅支持以下节点子类型:
*
    *整数值(类文字) *
  • 整数变量(类变量) *
  • 整数+运算符(类加号) *
  • 整数运算符(类减号) *
*它不支持整数以外的类型。 */ 公共类节点{ /** *将此AST编译为IJVM程序: *将指令附加到给定的程序中 *这样,指令在执行时 *产生相同的价值 *由调用evaluate()生成。 */ 公共无效生成代码(程序){ } /** *生成子树的字符串表示形式。 *在子类中实现此方法时, *尽可能使用对left.toString()和 *要执行此操作,请单击鼠标右键。 */ 公共字符串toString(){ 返回null; } } 公共类ILOAD扩展指令 { 私有最终整数值; 公共ILOAD(最终整数值) { 这个值=值; } 公共作废执行(存储){ storage.getStack().push(值); } /** *生成此指令的可读字符串表示形式。 */ 公共字符串toString(){ 返回“ILOAD”+值; } } 导入java.util.*; /** *在执行IJVM/Java字节码期间存储变量的空间。 */ 公共类可变空间{ 私有HashMap值; 公共变量空间(){ value=newhashmap(); } 公共void存储(字符串名称,int值){ this.value.put(名称、值); } 公共整型加载(字符串名称){ 返回值.get(name); } }
我遇到的问题是,在类变量中,我尝试将字符串转换为整数,因为类ILOAD需要整数。如果代码太长,我很抱歉,但是这些类是相互链接的。希望您能帮助我查看

根据那些
Integer.parseInt(字符串s)

投掷 NumberFormatException-如果字符串不包含可解析的 整数

在类
变量
的构造函数中,您正试图从一个不包含任何字符串的字符串中解析一个整数

public Variable(final String variable)
{
    this.variable = variable;
    int value = Integer.parseInt(variable);
}
…当你这样调用它时

new Variable("x")
请看下面的图片

根据那些
Integer.parseInt(字符串s)

投掷 NumberFormatException-如果字符串不包含可解析的 整数

在类
变量
的构造函数中,您正试图从一个不包含任何字符串的字符串中解析一个整数

public Variable(final String variable)
{
    this.variable = variable;
    int value = Integer.parseInt(variable);
}
…当你这样调用它时

new Variable("x")

错误信息很清楚

java.lang.NumberFormatException: For input string: "x"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:492)
您试图在parseInt()方法中将字符串“x”转换为整数,可能在这一部分

public Variable(final String variable)
    {
        this.variable = variable;
        int value = Integer.parseInt(variable);
    }

错误信息很清楚

java.lang.NumberFormatException: For input string: "x"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:492)
您试图在parseInt()方法中将字符串“x”转换为整数,可能在这一部分

public Variable(final String variable)
    {
        this.variable = variable;
        int value = Integer.parseInt(variable);
    }
您正在尝试将“x”解析为整数。您认为可能吗?绝对不可能!这就是它引发NumberFormatException的原因

阅读有关您正试图将“x”解析为整数的信息。您认为可能吗?绝对不可能!这就是它引发NumberFormatException的原因


阅读

您没有发布类的代码
VariableSpace
,因此我假设它是一种包含每个变量值的映射