This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

toString method  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble formatting this properly. So we are using Superhero names, so what I am about to show is an example of how it's supposed to be
The toString method needs to print it out like this

0 (280,Cat)
1
2
3
4
5
6
7
8 (498, Apple) -> (298, Boy)
9
its assumed that the last number of their ascii values is where they are placed and boy and apple will be replaced with superhero names


 
tyler ruterbusch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my test class

 
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does line 83 even compile?

As far as I can tell, addLast() only takes one argument: https://docs.oracle.com/javase/10/docs/api/java/util/LinkedList.html#addLast(E)

Your code attempts to pass in two arguments.
 
tyler ruterbusch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Does line 83 even compile?

As far as I can tell, addLast() only takes one argument: https://docs.oracle.com/javase/10/docs/api/java/util/LinkedList.html#addLast(E)

Your code attempts to pass in two arguments.




It does compile. I mean there are no errors. Also, my add last method does have two arguments? The key and word.
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad. I thought you were using a java.util.LinkedList, which also has an addLast() method but takes only one parameter.
 
tyler ruterbusch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:My bad. I thought you were using a java.util.LinkedList, which also has an addLast() method but takes only one parameter.



ahaha nah. we have to be super peculiar about things. We can't use built-in libraries. So we have to build things from scratch.
This is why the toString method is causing me some grief
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't know why you need N at all. You already initialize the bucket array to that length in the constructor so you can always use bucket.length where you're using N now.

Anyway, new StringBuilder(N) doesn't work the way you seem to think it works. myHashMap.length() will still be 0 so your for-loop won't even execute, not even once.

You chose a very poor name in myHashMap. First of all, that's a StringBuilder, not a HashMap. It's like declaring this:

What exactly do you want to iterate over anyway with that loop? Isn't it the bucket array?
 
tyler ruterbusch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
I don't know why you need N at all. You already initialize the bucket array to that length in the constructor so you can always use bucket.length where you're using N now.

Anyway, new StringBuilder(N) doesn't work the way you seem to think it works. myHashMap.length() will still be 0 so your for-loop won't even execute, not even once.

You chose a very poor name in myHashMap. First of all, that's a StringBuilder, not a HashMap. It's like declaring this:

What exactly do you want to iterate over anyway with that loop? Isn't it the bucket array?



Appreciate the tip. I want it to print it in the format above
each line being 0 - w/e and the (ascii value, word) next to the appropriate.


the length being based off the N of course.

 
tyler ruterbusch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Updated. Although the HM.append(bucket); is probably wrong.
I am just trying to take the pseudo logic in my head and turn it into code  
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? You changed myHashMap to just HM? So you changed from a name that misrepresented what it was to a name that makes that misrepresentation more obscure? In other words, in my Apple and Elephant analogy, instead of using a name like myElephant to misrepresent an Apple object, you choose to misrepresent it unclearly with a name like mE instead.

C'mon, man! You can do better!!!

Also, I indicated the new StringBuilder(someNumber) doesn't actually do what you think it does. At least read the API documentation and get an idea of what it actually does.
 
Junilu Lacar
Sheriff
Posts: 13392
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the very least, you can try it in JShell:

jshell> StringBuilder sb = new StringBuilder(50)

jshell> sb.length()
... ???

see what you get for the ??? part.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!