FAQ: Learn Java: Methods - The toString() Method

Hey guys, here is the error popup generated

We tested getting the toString() for a Lemonade store with a price of 1, and got the wrong string! expected:<…de at a price of 1.0[.]> but was:<…de at a price of 1.0>

This is my code for toString()

   //Using the toString() method
  public String toString(){
    return "This store sells " + productType + " at a price of " + price;
  }

you miss a full stop at the end of your sentence/string.

2 Likes

Amazing, lol, thank you, sir.

I’m getting the same error as megapro95987, but if I add a period, either within quotes or without, I get yet another error.
Store.java:34: error: expected
return "This store sells " + productType + " at a price of " + price.;
^
1 error
When I expand the window, the carat lines up under the semicolon.

Here’s the error with a period in quotes.
Store.java:34: error: ‘;’ expected
return "This store sells " + productType + " at a price of " + price “.”;
^
1 error

Can i see your full code so i can run it?

1 Like

Thanks stetim94, but I figured it out. I had forgotten to add the period to the statement with a “+”. Once I wrote it as " at a price of " + price + “.” it ran without a problem.

1 Like

The lesion should mention that toString() is a higher-level method, as this URL’s webpage explains.

Based on earlier method syntax one would expect the call in main to be System.out.println(lemonadeStand.toString() … which compiles/runs BTY. For the compiler to accept System.out.println(lemonadeStand) and still run toString implies something ‘under the hood’ we don’t know about.

2 Likes

return … + price + “.”;

… + price + “.”;

Thanks. I came in here wondering why I didn’t have to put .toString()

Yes, that’s an important information that was missing in this lesson. But this is a kinda recurring issue. I have the feeling sometimes things are not really told on really basic level but just “Do this and that will happen, isn’t this nice?” instead of “If you do this then that will happen because of this reason.”
For example for me it wasn’t clear that you can have only one constructor per class and that the constructor has to have the name of the root class which has to have the name of the file. I was wondering how you would assign methods to the object.
The concept of 1 file = 1 class = 1 object and everything within this file/class is part of this object wasn’t that clear from the get-go. Maybe I missed this somewhere, but I felt like you were just getting thrown into it and the explanation was added later.

You just summarized why Java is difficult first programming language to learn. There simply is a lot needed to get started

finding a balance between not overwhelming new learners (by cramming too much info into too few lessons) and missing out crucial information is very difficult.

2 Likes

Yeah, I totally get that!
In that case I suggest to make a note in the beginning (“Introduction to Classes”, first exercise).

I mean, OOP is not that difficult of a concept, what made me stumble in java was just this fact that 1 file/class = 1 object. Javascript for example handles this a little different, you can have multiple constructors/objects in one single file.

I remember when I first about OOP, I didn’t find it that easy. Combined with inheritance, method overwrite, constructors, controlling access to members of a class and building a good design, very difficult

but still just one constructor per class. And javascript classes. And JavaScript classes are also complicated, given how javascript deals with object and inheritance:

Inheritance and the prototype chain - JavaScript | MDN

Also, you raise an interesting point. Yes, we can have multiple objects within a single file in JS. Question: Do we want this? Structuring your code is very important, and Java enforces this where Javascript doesn’t

Trust me, you don’t want a file with 5000 lines of code and many objects. That is just a nightmare (one i haven’t ran into thankfully, hopefully it stays that way :slight_smile: )

anyway, we are getting a bit off-topic even though its an interesting discussion

Well, yeah. I mean, the concept itself is not that difficult, but to execute on it correctly is a challenge. Especially when you talk about root classes and how they may impact you inherited classes and how they affect them.

Hmm…while this is true I do like the other concept too. It allows you to approach it in a different manner. You might do a file per functioning sub-part of your program. While this will result in bigger files it may help in structuring your code better across multiple files. All objects needed for a functioning sub-part could be in one file instead of spread over multiple files.

And yes, Javascript object handing is interesting, I’m currently experimenting with Node-Red to get some stuff running and it has thrown a few rocks into my path.

2 Likes

I don’t see where the 'toString() Method is called.

Its literally stated in the exercise:

When we define a toString() method for a class, we can return a String that will print when we print the object

with example:

class Car {

    String color;

    public Car(String carColor) {
        color = carColor;
    }

    public static void main(String[] args){
        Car myCar = new Car("red");
        System.out.println(myCar);
    }

   public String toString(){
       return "This is a " + color + " car!";
   }
}

sure, there is no direct call, Java does this call under the hood. But the explanation and example should suffice.

Is the ‘under the hood’ only applicable to ‘toString()’ or are there a few others or too many to mention ?

Given the million of man hours that went into teaching a piece of rock to think, its safe to assume there is a lot happening under the hood.

1 Like