简体   繁体   中英

error: cannot find symbol while calling toString method

The assignment is asking me to:

"Design and implement the class CupDispenser. Each CupDispenser object has a location and keeps track of the number of cups it currently contains. Define two constructors, set and get methods for each data field, and the method toString. Also, define a method getOneCup that simply decrements the number of cups in the dispenser by 1. Demonstrate your class by creating several CupDispenser objects."

This is my class file:

public class CupDispenser
{
// Data Fields
private String location;
private int numberCups;

// Constructors
public CupDispenser(){
  location = "Unknown";
  numberCups = 0;
} 

public CupDispenser(String theLocation, int firstCups){
  location = theLocation;
  numberCups = firstCups;
}

// Methods
public String getLocation(){
  return location;
}

public void setLocation(String location){
  location = location;
}

public int getNumberCups(){
  return numberCups;
}

public void setNumberCups(int numberCups){
  numberCups = numberCups;
}

public void decrementNumberCups(){
  numberCups = numberCups - 1;
} 

//Other
public String toString(){
String result = ("Location: " + location + ". Number of cups: " + numberCups + ".");
return result.toString();
 }
}//end CupDispenser class

This is my demo file:

public class CupDispenserDemo{
public static void main(String[] args)
{
CupDispenser cd1 = new CupDispenser("My house", 10);
CupDispenser cd2 = new CupDispenser("Your house", 15);

System.out.println(cd1.toString);
System.out.println(cd2.toString);

cd1.setLocation("Her home");
cd1.setNumberCups(12);

cd2.decrementNumberCups();
cd2.decrementNumberCups();
}
}

In terminal I javac'd the CupDispenser.java class successfully, however the same can't be said for the CupDispenserDemo.java, as I get these errors:

CupDispenserDemo.java:14: error: cannot find symbol
 System.out.println(cd1.toString);
                     ^
  symbol:   variable toString
  location: variable cd1 of type CupDispenser
CupDispenserDemo.java:15: error: cannot find symbol
  System.out.println(cd2.toString);
                     ^
  symbol:   variable toString
  location: variable cd2 of type CupDispenser
2 errors

Could someone tell me what to do in order to fix these errors and other highly possible errors in my coding? To be honest, I'm not even sure what I'm doing, I'm just following other examples in the book that are similar to this question. Thank you so much for the help! This is getting troublesome for me as the class I'm taking moves so quickly through content. :/

toString() is not a variable, Its a method.

You forgot to add () for toString . Those two lines should be

System.out.println(cd1.toString());
System.out.println(cd2.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM