• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Printing hashmap

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Donielle Locke-Bowen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Donielle Locke-Bowen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic