aspose file tools*
The moose likes Beginning Java and the fly likes how to print hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to print hashmap" Watch "how to print hashmap" New topic
Author

how to print hashmap

harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
i want to print hash map for arrays of multiplication how to do it
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

harshal deshpande wrote:i want to print hash map for arrays of multiplication how to do it

Hi harshal. Welcome to javaRanch.

First: You need to describe your problem better. What is this HashMap? What values does it contain? (And BTW, HashMaps do NOT involve arrays - at least, not the way you think).

However, the first thing you might want to look at is the HashMap.toString() method. If that doesn't do what you want, come back and describe exactly what you want to see.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
Thanks sir.am trying to create code for generating multiplication table for number entered by user and entered numbers are in comma format.so i used hashmap here.i completed my code i just need to print it but its giving some error as
2,6,8
{2=[I@47b6617, 6=[I@416b724d, 8=[I@751d3ec8} instead of table i cant fix it and how to fix it
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
i have created this code but am getting error.sir am new to this kind of things please guide us i will be very thankful


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

harshal deshpande wrote:Thanks sir.am trying to create code for generating multiplication table for number entered by user and entered numbers are in comma format.so i used hashmap here.i completed my code i just need to print it but its giving some error as
2,6,8
{2=[I@47b6617, 6=[I@416b724d, 8=[I@751d3ec8} instead of table i cant fix it and how to fix it

You must be precise when asking for help; otherwise we're simply guessing. What you show suggests a HashMap of arrays, but without seeing some code it's difficult to know.

What error is it giving? - EXACTLY. Copy and paste the message.

And BEFORE YOU POST ANY CODE, please look at the UseCodeTags (←click) page.

Winston
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34


Sir its the code and its running but giving output wrong .not error message the output is wrong sir.i went to code and then edit and i dont know now sir i have to complete this part so please help me sir.i will read how to operate this forum everything about this at home now please help

1,2,3 {1=[I@4cdc135d, 2=[I@47b6617, 3=[I@416b724d} like this.why it giving output like this.and sir am new to java ranch so it will take some time to ask question correctly and post code.thanks sir

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

harshal deshpande wrote:Sir its the code and its running but giving output wrong .the output it is giving is
1,2,3 {1=[I@4cdc135d, 2=[I@47b6617, 3=[I@416b724d} like this.why it giving output like this.and sir am new to java ranch so it will take some time to ask question correctly and post code.thanks sir

Harshal, you didn't read my post; specifically the line "BEFORE YOU POST ANY CODE, please look at the UseCodeTags (←click) page."

You also haven't supplied the error message as I asked.

Please read the UseCodeTags page, then use the 'Edit' button to do what it tells you.

Winston
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

harshal deshpande wrote:
Sir its the code and its running but giving output wrong .not error message the output is wrong sir.

Hello Harshal,
EaseUp <-- You have to be patient if you want helpful answers..
According to your Code, the output is right.. Your are getting only what you are printing...


These are the hashcodes of the objects your are trying to print..
Now you have to figure out how and where you are printing objects.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
He did add the code tags, and got them all wrong, so I have corrected them.
And welcome to the Ranch

You get [code=java][/code] and you put the code, indentation and all, between ] and [; it is best to use ctrl‑C ctrl‑V. Don’t try to write it out by hand. On the editor it will look like this
[code=java]public class Foo
{
}[/code]

You need to explain what you wanted to see and what you are actually getting. I recommend you read this and this. I am not at all convinced that a Map is the best data structure to use, unless you hve been given this as an exercise in using Maps.
Your main method is much too long; it ought to be one statement long, if you want to write object‑oriented code.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

harshal deshpande wrote:1,2,3 {1=[I@4cdc135d, 2=[I@47b6617, 3=[I@416b724d} like this.why it giving output like this...

Because each value of your HashMap is an array, and the form "[I@4cdc135d" is what toString() prints out for an array.

Clearly, that's not what you want, so you need to tell us exactly what you DO want to see.

.and sir am new to java ranch so it will take some time to ask question correctly and post code.thanks sir

NoNeedToSaySorry. Learning how to ask questions properly takes a bit of time, but you'll get much better response when you do.
My suggestion: Read the HowToAskQuestionsOnJavaRanch (←click) page.

Winston
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
sir we want to generate multiplication table for numbers entered by user
example:if user enters numbers as 2,4,5 in comma separated form then the program must print the output in hash map for their multiplication table.
the program is correct but the multiplication table i.e table[j-1] in the method is not being collected in the return statement, so we are getting output something like this
{3=[I@47b6617, 6=[I@416b724d, 9=[I@751d3ec8}
I am not understanding how and where to fix it. please help
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

harshal deshpande wrote:but the multiplication table i.e table[j-1] in the method is not being collected in the return statement, so we are getting output something like this
{3=[I@47b6617, 6=[I@416b724d, 9=[I@751d3ec8}
I am not understanding how and where to fix it. please help

Firstly, that statement is wrong: It IS getting collected; it's just not printing out what you want.

And we CAN'T help you until you tell us exactly what you want to see. Show us examples of the output you want for at least two different tables.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Winston Gutkowski wrote: . . . the form "[I@4cdc135d" is what toString() prints out for an array. . .
Which is explained in the two links I posted earlier. Did you read them, HD? Did you understand them?
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
sir,i read that but the problem is that i have an array and i want to print it using Hashmap and how to do it for array.how to get all key values,how to iterate them sir its confusing???
its an assignment given to us by our teacher and i really dont know how to print it.
like multiplication table for 2 should be like {2=[2,4,6,8,10,12,14,16,18,20} i want output like this.i am begineer and they have given me so tough assignment i have to submit this today.how to do it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
harshal deshpande wrote:sir,i read that but the problem is that i have an array and i want to print it . . .
And which bit did you not understand? Don’t be scared to say it; we all were beginners once and we all know the JLS and API are not easy to read.

And do not try to do all your assignment at once. That is a mistake so many beginners make, trying to write lots of code. Then it becomes impossible to understand what is going wrong. You need to try tiny little bits, get them working and put them together. I shall remind you of the Java Tutorials section. That may help. Try my little classThat is proper object‑oriented code. Now you need to work out how to get the printing method to give you the desired display.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

harshal deshpande wrote:sir,i read that but the problem is that i have an array and i want to print it using Hashmap and how to do it for array.how to get all key values,how to iterate them sir its confusing???
its an assignment given to us by our teacher and i really dont know how to print it.
like multiplication table for 2 should be like {2=[2,4,6,8,10,12,14,16,18,20} i want output like this.i am begineer and they have given me so tough assignment i have to submit this today.how to do it

Are you going to get marks for it..

Well, jokes apart.. Look, in the links which campbell gave, two things are explained in detail (and these are the two things which you need to solve your problem)..
1). Arrays,
2). Object
What is bothering you is, Array is inside a HashMap..
Hint: - How you would have done that (printed the array elements), had it been outside the hashmap??
If you can get this done, you can also crack your problem.. Try out, you will feel better..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

harshal deshpande wrote:like multiplication table for 2 should be like {2=[2,4,6,8,10,12,14,16,18,20}...

Right. Finally you've shown us what you want to see.

First: HashMap has a method called entrySet(), which returns all the entries in the Map as a Set of Map.Entry's. That's your first step.

From there, Map.Entry.getKey() will return you the key (in the above case: '2') and Map.Entry.getValue() will return the array.

Next: Arrays.toString(int[]) will return a String containing the contents of your array in pretty much the exact form you describe above, viz:
"[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]"

So, now all you need is to put all that information together to put together a String in the format you want.

Here's a starter for you.
The following method will return a String in the format "2=[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" for a single entry in your HashMap:Now see if you can't use that to produce the String for the complete Map.

Winston
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
i just want to say thanks to everyone.am at home now so i will try to follow each and every valuable advice given by such friendly sirs.if you can guide me regarding java language i will be greatful to all of u.i want to cover oops and that is also correctly.some time am getting confused which is object and which is not.how to call methods? why we should avoid writting everything in main method.i am using head first and gosling but gosling is hard to understand.if you can recommend me with your style with your real time examples that would be great for understanding.
thanks in advance to all of you
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

harshal deshpande wrote:if you can guide me regarding java language i will be greatful to all of u.i want to cover oops and that is also correctly.some time am getting confused which is object and which is not.how to call methods? why we should avoid writting everything in main method.i am using head first and gosling but gosling is hard to understand.if you can recommend me with your style with your real time examples that would be great for understanding.

Harshal, that sounds like several questions; and not really related to the current one.
The rule here is: UseOneThreadPerQuestion (←click).

I'd start a new thread (or maybe one for each question; but be warned - responders tend to switch off if they think they're getting "machine-gunned").

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Which book is Gosling?

In addition to what Winston wrote, your posts are difficult to read because you don’t use punctuation, capital letters or even spaces before every word.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to print hashmap