首页 > 解决方案 > I need help making a java constructor that akes a string and initilizes the value var with appropriate in

问题描述

so sorry if my question is dumb but i haven't done java in 2 years. I'm working in my hw right now and i'm one method away from being done. but one of my constructor is wrong and idk where. thanks in advance! the value variable is a private integer. so far i have:

public  FancyInt(String a) {
       value = "";

       a = this.value;
}

basically this constructor takes a string and initalizes the value to the appropriate int

标签: javastringconstructor

解决方案


首先,如果你想将“值”等于“a”,你必须写:

this.value = a;

在 Java(和许多编程语言)中,您必须编写在等号之前更改的变量。

其次,如果您将“值”设为整数。您必须先将其更改为 int:

try {
   this.value = Integer.parseInt(a);
} catch (NumberFormatException e) {
    e.printStackTrace();
}

第三,如果“值”是一个整数。您必须用数字定义:

value = ""; //if value is an Integer, delete this line.

推荐阅读