Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Trouble printing my map's entry set with a function call

    Hi. I'm trying to figure out how get a void print method to work, so that I can print the entry sets of the maps in my Maps class with a function call.
    Here's the code:

    Maps.java
    Java Code:
    package samsExperiments;
    import java.util.HashMap;//HashMap<K,V>()
    import java.util.Map;
    import java.util.TreeMap;//TreeMap<K,V>()
    
    public class Maps {
    	
    	HashMap<Integer,String> myHashMap = new HashMap<>();
    	
    	public HashMap<Integer,String> myHashMap(){
    		HashMap phonebook = new HashMap();
    		phonebook.put(0, "715-225-8853");
    		phonebook.put(1, "715-333-6667");
    		phonebook.put(2, "715-852-6382");
    		phonebook.put(3, "715-839-7411");
    		return(phonebook);
    	}
    	
    	public void printHashMap() {
    		for(Map.Entry x : myHashMap.entrySet()) {
    			System.out.println("K : V = " + x.getKey() + " : " + x.getValue());
    		}
    	}
    	
    	TreeMap<Integer,String> myTreeMap = new TreeMap<>();
    	
    	public TreeMap<Integer,String> myTreeMap(){
    		TreeMap phonebook = new TreeMap();
    		phonebook.put(0, "715-225-8853");
    		phonebook.put(1, "715-333-6667");
    		phonebook.put(2, "715-852-6382");
    		phonebook.put(3, "715-839-7411");
    		return(phonebook);
    	}
    	
    	public void printTreeMap() {
    		for(Map.Entry x : myTreeMap.entrySet()) {
    			System.out.println("K : V = " + x.getKey() + " : " + x.getValue());
    		}
    	}
    }
    SamsExperimentsMain.java
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Collections;
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.TreeMap;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){
    		
    		Maps map = new Maps();
    		
    		HashMap<Integer,String> hashPB = map.myHashMap();
    		for(Map.Entry item:hashPB.entrySet()) {
    			System.out.println("K : V   =   " + item.getKey() + " : " + item.getValue());
    		}
    		System.out.println("Lets print the HashMap's keys and values againwith a function call. \n");
    		hashPB.printHashMap();
    		
    		TreeMap<Integer,String> treePB = map.myTreeMap();
    		for(Map.Entry item:treePB.entrySet()) {
    			System.out.println("K : V   =   " + item.getKey() + " : " + item.getValue());
    		}
    		System.out.println("Lets print the TreeMap's keys and values again with a function call. \n");
    		treePB.printTreeMap();				
    		
    	}//end of main method	
    	
    }//end of class
    In SamsExperimentsMain.java the errors are:

    Line 29, "The method printHashMap() is undefined for the type HashMap<Integer,String>"
    Line 36, "The method printTreeMap() is undefined for the type TreeMap<Integer,String>"

    What needs to be done so I can call those void print methods?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: Trouble printing my map's entry set with a function call

    The method printHashMap() is undefined for the type HashMap<Integer,String>"
    The compiler can not find that method in the HashMap class. Check the API doc for the HashMap class to find its valid methods.

    What needs to be done so I can call those void print methods?
    Use a reference to an instance of the class that defines the methods you want to call.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: Trouble printing my map's entry set with a function call

    Thanks norm. I figured it out:

    Java Code:
    package samsExperiments;
    import java.util.HashMap;//HashMap<K,V>()
    import java.util.Map;
    import java.util.TreeMap;//TreeMap<K,V>()
    
    public class Maps {
    	
    	public HashMap<Integer,String> myHashMap(){
    		HashMap phonebook = new HashMap();
    		phonebook.put(2, "715-225-8853");
    		phonebook.put(1, "715-333-6667");
    		phonebook.put(0, "715-852-6382");
    		phonebook.put(3, "715-839-7411");
    		return(phonebook);
    	}
    	
    	public void printHashMap(Map<Integer,String> myHashMap) {
    		if(myHashMap.isEmpty()) {
    			System.out.println("The map is empty.");
    		}
    		else {
    			for(Map.Entry x : myHashMap.entrySet()) {
    				System.out.println("K : V = " + x.getKey() + " : " + x.getValue());
    			}
    		}
    		
    	}
    	//How to call it in code:
    	/*
    	Maps map = new Maps();
    	HashMap<Integer,String> phonebook = new HashMap<>();
    	phonebook = map.myHashMap();
    	map.printHashMap(phonebook);
    	*/
    	
    	public TreeMap<Integer,String> myTreeMap(){
    		TreeMap phonebook = new TreeMap();
    		phonebook.put(2, "715-225-8853");
    		phonebook.put(1, "715-333-6667");
    		phonebook.put(0, "715-852-6382");
    		phonebook.put(3, "715-839-7411");
    		return(phonebook);
    	}
    	
    	public void printTreeMap(Map<Integer,String> myTreeMap) {
    		if(myTreeMap.isEmpty()) {
    			System.out.println("The map is empty.");
    		}
    		else {
    			System.out.println(myTreeMap);//prints entries horizontally
    		}
    		for(Map.Entry x : myTreeMap.entrySet()) {
    			System.out.println("K : V = " + x.getKey() + " : " + x.getValue());
    		}
    	}
    	//How to call it in code:
    	/*
    	Maps map = new Maps();
    	TreeMap<Integer,String> phonebook2 = new TreeMap<>();
    	phonebook2 = map.myTreeMap();
    	map.printTreeMap(phonebook2);
    	*/
    }

Similar Threads

  1. How to call a VBA function in Java
    By gorgon777 in forum New To Java
    Replies: 0
    Last Post: 01-20-2014, 01:24 PM
  2. call a function dynamically
    By bigjo in forum New To Java
    Replies: 3
    Last Post: 01-18-2013, 03:56 PM
  3. Call a function via URL
    By cali_guy_0024 in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 05:04 AM
  4. Printing user's entry.
    By mConfused in forum Advanced Java
    Replies: 1
    Last Post: 04-03-2010, 04:00 PM
  5. function call error
    By peter_thm in forum New To Java
    Replies: 2
    Last Post: 01-13-2010, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •