char c = 0; //is not declared and initalized
number = Integer.parseInt(decode(c));
문제
I have received the following error message
G:\CIS260\Assignments>javac PhoneNumber.java
PhoneNumber.java:45: error: incompatible types
number = decode(c);
^
required: int
found: String
1 error
in the beginning of the class i have
char c;
private int number = 0
This makes it an int so i understand that in order for the next line to compile i have to have two of the same data types. My understanding is that
str[1].valueOf(number);
number = decode(c);
public static String decode(char c){
switch (c) {
should make the variable NUMBER a string making decode and number equal data types because they are both strings.
I feel like i may be forgetting a step in order to make both strings data types. any thoughts?
해결책
char c = 0; //is not declared and initalized
number = Integer.parseInt(decode(c));
다른 팁
yes you are declaring number as Integer.
so you should cast it by using Integer.ParseInt.
You have the method public static String decode(char c)
which returns a value of String
, but the variable number
accepts the value String
which is declared as an Integer
Try having a return type of Integer
like public static int decode(char c)
Your method returns a String
.
You are trying to store that returned string in an int
.
Java does not convert strings to integers automatically like this. You will have to do the conversion explicitly. You can use Integer.parseInt
for this:
int number = Integer.parseInt(decode(c));
Note that a NumberFormatException
will be thrown if the returned string isn't actually a valid integer.