3

I am trying to read in values to a HashMap from a Driver program to a separate abstract program. After that, I want the contents to be printed to the user. However, when I call the method nothing happens

GameDriver

import java.io.*;
import java.util.*;
import java.awt.*;

public class GameDriver {

    public static void main(String[] args) throws FileNotFoundException {

        String s;
        File f = new File (args[0]);
        Scanner input = new Scanner(f);
        while (input.hasNext()) {
            s = input.next();
            if (s.equalsIgnoreCase("h")) { 
                Hero c1 = new Cop();
                System.out.println(c1); 
            }

            if (s.equalsIgnoreCase("r")) { 
                Hero c2 = new Cop();
                String cn = input.next();
                int pts = input.nextInt();
                c2.newMap.put(cn, pts);
                c2.rescue();
                System.out.println(c2);
            }

        }

    }
}

Hero.java

import java.util.*;
import java.io.*;
import java.awt.*;

public abstract class Hero extends Character
{
   private String heroname1;
   public Hero() {
          heroname1 = "Rick Grimes"; //the default player name
   }
   HashMap<String, Integer> newMap = new HashMap<String, Integer>();

   public Hero(String newhero) {
          if (newhero.length() > 0) {
               heroname1 = newhero;
          } else { heroname1 = "Rick Grimes"; } //defaulted as protagonist
   }

   public String getHeroName() {
          return heroname1; //return the name
   }

   public String rescue() { //class to rescue people or things
          return " rescued " + newMap + "pts!";
   }

   public String toString() { //print
          return heroname1 + rescue();

   }
}

The Driver reads in a .txt file here is a sample

.TXT

c h Rick Grimes//create hero
r Carl 100 //rescue name and point value

and finally my output vs desired output

Current Output

Rick Grimes
Rick Grimes

Desired Output

Rick Grimes
Rick Grimes rescued Carl 100pts!

Thank you to anyone who helps! Ask if you need any clarification

EDIT Cop.java

import java.io.*;
import java.util.*;
import java.awt.*;

public class Cop extends Hero {
   public Cop() {
         super();
   }
   public void hero1(String newhero) {
          newhero = getHeroName(); //get name from the Hero class
   }
   public void lieDetect() { //unique ability for cops
          System.out.println("Cops can tell the good from the bad");
   }
}
Michaël
  • 3,679
  • 7
  • 39
  • 64
Junikin
  • 301
  • 2
  • 14
  • 1
    Add the code of your Cop Class. It could help. – Michaël Dec 18 '14 at 07:16
  • Ok, added to my edit – Junikin Dec 18 '14 at 07:18
  • 1
    related: http://stackoverflow.com/questions/27518563/inheritance-proper-initialization-to-store-values-into-a-hashmap ?? –  Dec 18 '14 at 07:20
  • That question was sorta answered but yes you can find all my code there. It's a little updated though on here – Junikin Dec 18 '14 at 07:22
  • Have you run your code? Because I have done it and I obtain the following output : `Rick Grimes rescued {}pts! Rick Grimes rescued {Carl=100}pts!` It's almost what you are expecting. – Michaël Dec 18 '14 at 07:31
  • That is very strange. When I ran it earlier it gave me what I have under current output. Do you mind if I change my question then? I still just want what is under my desired output, meaning the first line of output only saying `Rick Grimes` – Junikin Dec 18 '14 at 07:35

1 Answers1

1

In GameDriver :

if (s.equalsIgnoreCase("h")) { 
    Hero c1 = new Cop();
    System.out.println(c1.getHeroName()); 
}

The rescue method in the class Hero :

public String rescue() { //class to rescue people or things
       String toReturn = "";
       for(String _key : newMap.keySet()) {
           toReturn += " rescued " + _key + " " + newMap.get(_key) + "pts!";
       }
       return toReturn;
   }

And I obtain the desired output.

Michaël
  • 3,679
  • 7
  • 39
  • 64