aspose file tools*
The moose likes Java in General and the fly likes How to code program using API Hashmap to return two subsequently allocated strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to code program using API Hashmap to return two subsequently allocated strings " Watch "How to code program using API Hashmap to return two subsequently allocated strings " New topic
Author

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

Mary Dawson
Ranch Hand

Joined: Feb 08, 2009
Posts: 36
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.


A man of words and not of deeds is like a garden full of weeds, and when the weeds begin to grow, its like a garden full of snow, and when the snow begins to fall- its like a bird upon the wall and when the bird begins to fly, its like an eagle in the sky
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Edward Nelson
Greenhorn

Joined: Apr 29, 2008
Posts: 8
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

Joined: Feb 08, 2009
Posts: 36
Thank you, Abu, it works great.
Edward Nelson
Greenhorn

Joined: Apr 29, 2008
Posts: 8
You're welcome!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to code program using API Hashmap to return two subsequently allocated strings