Creating a TreeMap within a TreeMap.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • millz
    New Member
    • Sep 2007
    • 2

    #1

    Creating a TreeMap within a TreeMap.

    I am trying to create a tree map within a TreeMap.
    Any help would be great.
    goes a bit like this:

    [code=java]
    public class ChessImpl implements CHESS
    {
    private Map<String, Integer> name;
    private int membernumber;
    private Map<String, Map<Integer, Holding>> stock;
    private Map<Integer, Holding> intSearch;
    private Holding holder;


    public ChessImpl()
    {
    name = new TreeMap<String, Integer>();
    membernumber = 1;
    stock = new TreeMap<String, Map<Integer, Holding>>();
    }
    [/code]

    With the stock map do I need to create another map before i insert it?
    Main problem is when i use the put method it wont accept it?

    [code=java]
    public void issue(String stockCl, String client, int nshares)
    {
    if(name.contain sKey(client) && (nshares > 100))
    {
    Holding holder = new Holding(stockCl , client, nshares);
    int memNum = name.get(client );
    stock.put(stock Cl, (memNum, holder)) //THIS IS WHERE IM HAVING THE MAJOR PROBLEM
    }
    else
    {
    System.out.prin tln("Client name has not been created or the amount of shares is less than 100");
    }
    [/code]

    I keep getting a error message. this is the holding class just for refrence.

    [code=java]
    public class Holding
    {

    private String stock;
    private String client;
    private int shares;
    public Holding(String stock, String client, int shares)
    {
    this.stock = stock;
    this.client = client;
    this.shares = shares;
    }
    [/code]
    Last edited by JosAH; Sep 17 '07, 02:43 PM. Reason: added code tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by millz
    I keep getting a error message.
    What is the exact error message? Is it a compilation error or a runtime error?

    kind regards,

    Jos

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by millz
      I am trying to create a tree map within a TreeMap.
      Any help would be great.
      goes a bit like this:

      [code=java]
      public class ChessImpl implements CHESS
      {
      private Map<String, Integer> name;
      private int membernumber;
      private Map<String, Map<Integer, Holding>> stock;
      private Map<Integer, Holding> intSearch;
      private Holding holder;


      public ChessImpl()
      {
      name = new TreeMap<String, Integer>();
      membernumber = 1;
      stock = new TreeMap<String, Map<Integer, Holding>>();
      }
      [/code]

      With the stock map do I need to create another map before i insert it?
      Main problem is when i use the put method it wont accept it?

      [code=java]
      public void issue(String stockCl, String client, int nshares)
      {
      if(name.contain sKey(client) && (nshares > 100))
      {
      Holding holder = new Holding(stockCl , client, nshares);
      int memNum = name.get(client );
      stock.put(stock Cl, (memNum, holder)) //THIS IS WHERE IM HAVING THE MAJOR PROBLEM
      }
      else
      {
      System.out.prin tln("Client name has not been created or the amount of shares is less than 100");
      }
      [/code]

      I keep getting a error message. this is the holding class just for refrence.

      [code=java]
      public class Holding
      {

      private String stock;
      private String client;
      private int shares;
      public Holding(String stock, String client, int shares)
      {
      this.stock = stock;
      this.client = client;
      this.shares = shares;
      }
      [/code]
      Quick answer: Yes, you must create an Object, before using it. Instead of
      [CODE=java]
      stock.put(stock Cl, (memNum, holder));
      [/CODE]you want something like
      [CODE=java]
      TreeMap tmpMap = new TreeMap();
      tmpMap.put(memN um, holder);
      stock.put(stock Cl, tmpMap);
      [/CODE]That should do the job.

      Greetings,
      Nepomuk

      Comment

      • millz
        New Member
        • Sep 2007
        • 2

        #4
        Thanks for the suggestions ill try that code 2nite.

        Comment

        Working...