Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having trouble running public HashMap method

The purpose and goal of the method is to print the hash map in reverse from <Key, Value> to <Value, Key>. But I’m having a problem on line 7(revrse();) where where I get the error "The method reverse(Map<Integer,String>) in the type lab8_Part3 is not applicable for the arguments ()". All the solutions eclipse suggest result in an error and I’m stumped. Help is appreciated

import java.util.HashMap;
import java.util.Map;

public class test {

    public static void main(String[] args) {
        reverse();
    }

    public Map<String, Integer> reverse(Map<Integer, String> map) {
        HashMap<String, Integer> reversed = new HashMap<String, Integer>();
        reversed.put("Eightenn", 18);
        reversed.put("Fifty", 50);
        reversed.put("neThreeTwo", 132);
        reversed.put("Ocho", 8);
        reversed.put("Forty-one", 41);
        System.out.println("Reversered map " + reversed);
        for(int key : map.keySet()) {
            String value = map.get(key);

            if(!reversed.containsKey(value)) {
                reversed.put(value, key);
            }
        }
        return reversed;
    }
}

The purpose and goal of the method is to print the hash map in reverse from <Key, Value> to <Value, Key>.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The reverse() method expects Map<Integer, String> parameter and return a Map<Integer, String> object, but you’re calling it without any arguments and without assigning the object.

I modified your code, this should work.

import java.util.HashMap;
import java.util.Map;

public class test {

    public static void main(String[] args) {
        Map<Integer, String> original = new HashMap<Integer, String>();
        original.put(18, "Eightenn");
        original.put(50, "Fifty");
        original.put(132, "neThreeTwo");

        Map<String, Integer> reversed = reverse(original);
        System.out.println("Reversed map: " + reversed);
    }

    public static Map<String, Integer> reverse(Map<Integer, String> map) {
        Map<String, Integer> reversed = new HashMap<String, Integer>();
        for(int key : map.keySet()) {
            String value = map.get(key);
            reversed.put(value, key);
        }
        return reversed;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading