0

Basically I'm making this Java program in BlueJ, in which the player is in the world of The Lord of the Rings. I created separate packages for weapons, items etc. I have a class Main outside all the packages(in the main body of the project screen). In there, I tried something.

public static void test()throws Exception{
        System.out.println("There is a brass sword and an iron sword. Which do you want?");
        Scanner in = new Scanner(System.in);
        String s = in.next();
        HashMap options = new HashMap();
        options.put("brass", new Sword());
        options.put("iron", new Sword());
        Sword k = options.get(s);
}

I want the above method to return a Sword object to me. This does not work, unfortunately. Any help....?

3
  • 4
    Use parameterized type HashMap. Commented Oct 31, 2013 at 10:27
  • 1
    Also, never ever put a "throws Exception" at the end of your method, please. If your method may throw a checked exception, create a new Exception class and throw it. Commented Oct 31, 2013 at 10:30
  • @Rohit: Still doesn't work.. Commented Oct 31, 2013 at 10:31

4 Answers 4

2

Just use the parametrized type HashMap, declare the HashMap as

HashMap<String, Sword> options = new HashMap<String, Sword>();

I want the above method to return a Sword object to me.

Then change the method return type and add a return to it:

public static Sword test()throws Exception{
        System.out.println("There is a brass sword and an iron sword. Which do you want?");
        Scanner in = new Scanner(System.in);
        String s = in.next();
        HashMap<String, Sword> options = new HashMap<String, Sword>();
        options.put("brass", new Sword());
        options.put("iron", new Sword());
        Sword k = options.get(s);
        return k;
}
Sign up to request clarification or add additional context in comments.

3 Comments

and he needs to change void to Sword
@R.J Yes I didn't notice the last part of the question, I updated, thanks
Since this seems to be "test code", parts of which may actually get copied to actual code, I'd prefer using interface as type Map<String, Sword> options = new HashMap<String, Sword>().
1

Use following code:

public static Sword test()throws Exception{
    System.out.println("There is a brass sword and an iron sword. Which do you want?");
    Scanner in = new Scanner(System.in);
    String s = in.next();
    HashMap<String, Sword> options = new HashMap<String, Sword>();
    options.put("brass", new Sword());
    options.put("iron", new Sword());
    return options.get(s);
}

Comments

0

if you want your method to return a Sword object you should change public static void test() to public static Sword test() and at the end of your method call return sword

Comments

0

The default hashMap accepts two generic types HashMap<Object,Object> which represents HashMap<Key,Value> with your code you will have to cast the options.get(s) to Sword but with that you don't use the power of Generics so recommend the @BackSlash's answer since you won't need casting.

More on Generics:http://www.tutorialspoint.com/java/java_generics.htm

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.