tony tony - 1 month ago 10
Java Question

char cannot be dereferenced

I am working on an assignment which need to use get and set method to convert the temperature. However, when I try to write the setMethod, U got an error said that " char cannot be dereferenced". Here is my code;

public void setTemp(double temp, char scale){
// - sets the objects temperature to that specified using the
// specified scale ('K', 'C' or 'F')
if (scale.isLetter("K")){
temp = temp + 273;
}else if (scale.isLetter("C")){
temp = temp;
}else if (scale.isLetter("F")){
temp = ((9 * temp) / 5 ) + 32;
}
}

Answer

Primitives (such as char) don't have method. It seems, though, that you're just looking for a simple equality test.

Edit:
As Elliott Frisch noted in the comments, you need to use this.temp to reference your data member as the temp argument is hiding it:

public void setTemp(double temp, char scale){
    // - sets the objects temperature to that specified using the 
    //   specified scale ('K', 'C' or 'F')
    if (scale == 'K'){
      this.temp = temp + 273;
    } else if (scale == 'C') {
      this.temp = temp;
    } else if (scale == 'F') {
      this.temp = ((9 * temp) / 5 ) + 32;
  }
}