This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

TestPizza.java

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a method called toString() which takes the name, toppings and price of a pizza all at once. This requires me to convert the toppings and price from their respective data types to strings.

However, I'm receiving the errors below:

The method String(String[]) is undefined for the type Pizza
The method String(double) is undefined for the type Pizza

I understand the error is something to do with trying to convert to string but don't know how to fix it.

I'm also receiving errors in the main method relating to my curly brackets where the arrays of ingredients are.

Code for Pizza.java:




Code for the main method:

 
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't have multiple return statements like that in a method. The compiler will recognize that only the first return statement will ever be executed and flag the other return statements as unreachable code, which is illegal.

Search for how to override toString() in Java
 
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid that isn't the Java® way to print things. What you do is give your classes a toString() method. Actually all objects already have such a method because it exists here, and you have to override it. I see you have had a go, but you won't get that to work. You can't write code after return. I suggest you try something like this:-If you look here you will find that @Override is there to catch any spellling errors and similar. You will unfortunately be disappointed by the output because the array won't come out at all nicely. But all you need to do now is write System.out.println(myPizza); and you will get most of the info you want

Watch this space for more info.
 
Sheriff
Posts: 7423
505
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remove those comments you have. They don't tell anything new.

Well, maybe with an exception of that one: "//the toppings required by this pizza", in particular the word "required" caught my attention. If that's really true, you should then validate in constructor that toppings array isn't empty.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by toString(pepperoniPizza) you meant to call the toString() method of the Pizza object referenced by the pepperoni variable, then that's not how to do it. You have it backwards.

This is how you invoke/call a method on an object:

That code, however only produces a String and does nothing else. If you want to display the String value produced, then you have to do something like this:

However, you can simplify that even further because the System.out.println() method will invoke the toString() method of any object you pass to it, so all you really need to write is this:
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are all sorts of ways to turn an array into a String. Just a few:-That line 4 removes the [] from the ends of the output.Maybe StringJoiner is the best solution because you don't have to remove the last comma or anything. See that link for an example of its use.

[addition]If you are already dealing with Strings, the + operator will implicitly cause toString() to be called. [Another addition]That previous sentence is not quite 100% accurate.
 
Charles Ormond
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:



Could you explain what you mean when you say "internally invoke toString()"?

This seems like it would defeat the point of having the toString method which I have to use to hit the task objectives below:

Staff note (Junilu Lacar):

Please quote only the parts that are relevant to your question. Quoting entire posts, especially long ones, only produces more noise and clutter in the thread.

 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles Ormond wrote:. . . Could you explain what you mean when you say "internally invoke toString()"? . . .

Find the link to System.out. Here it is. Then this link to println(Object). That quotes String#valueOf(Object), and that link tells you it calls toString().
So you get what you wrote in toString() used by System.out.println() and similar.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"interally invokes toString()" simply means that somewhere in the System.out.println() method is code that calls the object's toString() method. Something to this effect:

You'll have to dig into the source code to see what's actually been written but in essence, that's what happens when you call System.out.println(someObject), that someObject.toString() will get called in there at some point.

The point of you writing your own version of toString() is so that instead of the default behavior provided by the primordial Object.toString() which often looks like gibberish, you can instead display something more meaningful. It's not at all "defeating the point" as you said. It's actually taking advantage of how other code that has already been written would use toString(). You are providing custom behavior that will take advantage of inheritance and polymorphism to introduce new and better behaviors.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a quick example to illustrate what that exercise is trying to get you to learn:

If you run this program, you'll get what appears to be gibberish. What you're seeing is the value returned by Object.toString() which is inherited by the Dog class.

Now, if you override that like so:

then run the TestDog program again, you'll see something different. So overriding toString() is not pointless. I'm just using the fact that System.out.println() already calls toString() anyway, so I can just write System.out.println(ruff) instead of System.out.println(ruff.toString()), which is redundant.
 
Charles Ormond
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have now edited the code to the following:



and the Main Method




The only error I seem to be having now is

Syntax error on token "{", delete this token

in the main method around the arrays.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't pass array literals like that. You'd have to write new String[] { ... } instead of just { ... }. Like so:
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you're going to find that your toppings will look like gibberish. That's because of how the toString() for arrays works. Use Arrays.toString() instead, or see one of Campbell's previous suggestions for how to create a String representation of an array.
 
Charles Ormond
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Also, you're going to find that your toppings will look like gibberish. That's because of how the toString() for arrays works. Use Arrays.toString() instead, or see one of Campbell's previous suggestions for how to create a String representation of an array.



I've updated the code to



And the output for the array is as you said (seems to be a memory location or something similar).

So I should change the code to the below then?










 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles Ormond wrote:. . . So I should change the code to the below then? . . .

No. You'll never get that past the compiler. Have another look at this post of Junilu's. Go back to your post one before that. All you need to do is insert new String[] at the appropriate point and that line will be all right. Except that it will be too long. Have a look at the two sections (4.1‑4.2) in the old Sun Style Guide starting here; that should tell you one way to break long lines into smaller parts.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles Ormond wrote:So I should change the code to the below then?


No. Everything you need to do is inside the toString() method, remember?

#1: If you write System.out.println(someArray), you'll get gibberish, right?

#2: If you write System.out.println(Arrays.toString(someArray)), you'll get something more legible.

#3: If you write "Pepperoni " + someArray + " some other stuff" you'll get a string with gibberish where the array values are supposed to be.

#4: If you write "Pepperoni " + Arrays.toString(someArray) + " some other stuff" you'll get a more legible string.

I hope you're starting to see the pattern of how these things work. So, if your toString() is essentially doing what #3 above is doing, what change should you make to it so that it gives you back something more legible?
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose it might be helpful to explain how the + (String concatenation) operator works with mixed type arguments.

The compiler basically translates any expression that uses the + operator to calls to a StringBuilder when it sees that any of the terms is a String. That is, this expression:

results in the compiler doing something like this behind the scenes:

There's a nuance to this, however. This:

is equivalent to this:

however this:

Gives something different. It is equivalent to this:

See if you can figure out the difference and what the rules are. One last note: just like System.out.println(Object), StringBuilder.append(Object) also calls Object.toString() internally.
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another thing you can try to get a better idea of how + (String concatenation) works:

Try the following in jshell (the Java REPL):

jshell> 3 - 1 + " times"

... what happens?

jshell> "times " + 3 - 1

... what happens now?

What's the difference between the two expressions?
 
Junilu Lacar
Sheriff
Posts: 14767
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know learning a programming language like Java can be tough and confusing but try to think logically here.

If this doesn't work: new Pizza("Regular", {"Cheese", ... }, ...)

and this does new Pizza("Regular", new String[]{"Cheese", ...} , ...),

then why would you think that new Pizza("Regular", {"Cheese", ...}.toString(), ...) could work?

It's like you took one step forward and then immediately took two steps back in your understanding. You can't guess your way around an error, you have to understand why what you wrote is not acceptable.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!