This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Printing hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing hashmap" Watch "Printing hashmap" New topic
Author

Printing hashmap

Donielle Locke-Bowen
Greenhorn

Joined: Sep 24, 2009
Posts: 6
For our homework assignment, we have to write a program that reads integers from a specified file. If there are duplicate entries, we need to output them as well as they're locations. If not, we need to state that there aren't any duplicates. I've gotten most of the code below:



The program compiles fine, but when I run it, needless to say nothing outputs - there's no println. I have no clue how to actually print the results. Everytime I add a println I get a compiling error. What's the syntax for printing Hashmaps? HELP!!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Everytime I add a println I get a compiling error.


It may be a good idea to start with what have you tried -- if you tell us, along with the error, we can tell you what you are doing wrong.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Donielle Locke-Bowen
Greenhorn

Joined: Sep 24, 2009
Posts: 6
Here's my code. Everything I've added has comment lines next to it. I added and removed each line when one wouldn't work.



I've even done printCollection and printMap. Both have come back with compiler errors.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

System.out.println(m.put(x))


This won't compile. There is no put() method that takes one parameter.

I've even done printCollection and printMap. Both have come back with compiler errors.


Not enough details. What do you mean you "done printCollection and printMap"? And what compile errors?

Henry
Donielle Locke-Bowen
Greenhorn

Joined: Sep 24, 2009
Posts: 6
printCollection(x);
printCollection(scan);
printCollection(Map);
printCollection(HashMap);
printMap(x);
printMap(scan);
printMap(Map);
printMap(HashMap);

I've added all of these and have gotten "cannot find symbol"

I've never compiled complex java assignments like this, please be patient with me. Thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Donielle Locke-Bowen wrote:
I've added all of these and have gotten "cannot find symbol"


Did you create those methods? Those methods don't exist in the API, so you if want them, you'll have to create them first.

Henry
Donielle Locke-Bowen
Greenhorn

Joined: Sep 24, 2009
Posts: 6
Henry Wong wrote:
Donielle Locke-Bowen wrote:
I've added all of these and have gotten "cannot find symbol"


Did you create those methods? Those methods don't exist in the API, so you if want them, you'll have to create them first.

Henry


I just looked at examples from my textbook, which used both printMap and printCollection. Clearly I'm on the wrong track. I need serious help with this. I have no clue where to go from here.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

I just looked at examples from my textbook, which used both printMap and printCollection.


Book examples generally don't use stuff that doesn't exist. So, are these methods presented in earlier chapters? Or are the methods for the examples available for download somewhere?

Henry
Stefan Brandenberger
Greenhorn

Joined: Aug 31, 2009
Posts: 12
Donielle Locke-Bowen wrote:What's the syntax for printing Hashmaps? HELP!!!

Hi Donielle

would do that for you.

But thats not what you really want .

You build a Map with the found integers as key and the number of their occurences as value. So, when you're finished with building the map, you have to loop over it (hint: Map.entrySet()) and print those keys with an associated value > 1. But maybe your textbook has examples for that.

Further: Your Scanner takes a String as constructor-argument. So what you actually are scanning is the string "assignment.txt" and not the content of a file with that name. Fortunately there's also a constructor that takes an InputStream. So all you have to do is first create a FileInputStream on your "assignment.txt"-file.

Stefan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
And welcome to JavaRanch
Donielle Locke-Bowen
Greenhorn

Joined: Sep 24, 2009
Posts: 6
Hi Donielle

would do that for you.

But thats not what you really want .

You build a Map with the found integers as key and the number of their occurences as value. So, when you're finished with building the map, you have to loop over it (hint: Map.entrySet()) and print those keys with an associated value > 1. But maybe your textbook has examples for that.

Further: Your Scanner takes a String as constructor-argument. So what you actually are scanning is the string "assignment.txt" and not the content of a file with that name. Fortunately there's also a constructor that takes an InputStream. So all you have to do is first create a FileInputStream on your "assignment.txt"-file.

Stefan

Here's my new code:



Now I'm getting this:
Duplicate4.java:15: cannot find symbol
symbol : method hasNextInt()
location: class java.io.InputStream
while(myFileStream.hasNextInt())
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
The hasNextInt() method belongs to the Scanner class not the input stream class.

There is an example quite similar to what you want near the beginning of the Scanner class in the API documentation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing hashmap