• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to code program using API Hashmap to return two subsequently allocated strings

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Programmers,
Forgive my lack of knowledge regarding correct programming for API collections Hash Map, but I can't quite get it right, as I wanted program to submit the placed Strings - including the ISBN numbers, yet as coded, program only submits the authors, I can't get it to give the ISBNs - is it that next() method thats forcing it to skip alternating strings - and/or can I put in another method that will output the ISBN strings as stored ?
The output order is as Exercise stipulates - so its half right, I suppose.
[CODE]
import java.util.*;

public class MyMap
{
public static void main(String [] args)
{
Map myHashMap = new HashMap();
myHashMap.put("James Joyce", "111-12-1212");
myHashMap.put("Mai Li", "222-21-2121");
myHashMap.put("Murray Kaye", "333-31-1313");
myHashMap.put("Sam Adams", "444-41-4141");
myHashMap.put("Juanita Perez", "555-51-1515");

// Create Iterator and while loop


Iterator myIterator;
myIterator = myHashMap.keySet().iterator();

while (myIterator.hasNext())

{
String myKey;
myKey = (String)myIterator.next();
System.out.println(myKey);
}



}
}


[//CODE]

gives:-

[CODE]

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\user>set PATH=C:\jdk1.42\jdk\bin

C:\Documents and Settings\user>cd C:\lab_files\Lesson_7


C:\lab_files\Lesson_7>javac MyMap.java

C:\lab_files\Lesson_7>java MyMap
James Joyce
Murray Kaye
Juanita Perez
Mai Li
Sam Adams


[//CODE]

Thank you for your help in advance
Yours
Mary Dawson.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only print the keys--you never retrieve anything from the map to print.

Also note that your code tags didn't work--I've taken to "preview"ing my posts as a sanity check so any really obvious errors I've made are caught before submitting.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mary Dawson wrote:



Using the following println statement should help:


myHashMap.get(myKey) will retrieve the value corresponding to the current key.

Also, consider parameterizing your generic types (i.e. Map and HashMap should be Map<String, String> and HashMap<String, String>) for better type checking.
 
Mary Dawson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Abu, it works great.
 
Edward Nelson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic