views:

46

answers:

3

I'm trying to make a class where I put a key and value into the put method which puts the key in the k string array and value into the v string array, however it is not being saved in the array when I do get or display.
For example: put(dan,30) get(dan) returns null
display returns null null 10 times. Does anyone know what's wrong?

public class Memory 
       {
       final int INITIAL_CAPACITY = 10;
       String[] k = new String[INITIAL_CAPACITY];
       String[] v = new String[INITIAL_CAPACITY];
       int count = 0;

       public Memory()
       {
          count = 0;
       }
       public int size()
       {
          return count;
       }
       public void put(String key, String value)
       {
            int a = 0;
            boolean found = false;
            for (int i = 0; i < k.length; i++)
            {
                //System.out.println("key is " + key.equals(k[i]));
                if (key.equalsIgnoreCase(k[i]))
                {
                    v[i] = value;
                    found = true;
                }
                if (found)
                    break;
                a++;
            }
            //System.out.println(a == k.length);
            if (a == k.length);
            {
              k[count] = key;
              v[count] = value;
            //System.out.println(k[count] + " " + v[count]);
              count++;          
            //System.out.println(count);
            }

       }
       public String get(String key)
       {
          String output = "a";
          for(int i = 0; i < k.length; i++)
          {
             if(!key.equalsIgnoreCase(k[i]))
             {
                output = null;
             }
             else
             {   
                output = v[i]; 
                return output;
             }  
          }
          return output;
       }
       public void clear()
       {
            for (int i = 0; i < k.length; i++)
            {
                k[i] = null;
                v[i] = null;
            }
          count = 0;
       }

        public void display()
        {
            for (int i = 0; i < k.length; i++)
            {
                System.out.println(k[i] + " " + v[i]);
            }
        }
    }
+1  A: 

if (a == k.length);

remove the semicolon or else it always runs the block below

And I hope this is just as a study, because you should just use Map and not implement one yourself. Also your algorithm is very poor. It is O(n) performance for both put and get. You can actually do better like O(log n)

Pyrolistical
The for loop in put method checks if key is already in the array, and if it is, it will change the value
Raptrex
you don't understand. the line `if (a == k.length);` can be removed and your code will do exactly the same thing. its an error because if you put and match on an existing key you'll also add that to the end of the array and increment count. then you'll have two keys with the same value.
Pyrolistical
On the same line maybe remove the 'found' variable and just break out of the loop if the key is already in the array
pgmura
A: 

Your code worked for me.

Just ran:

public static void main(String args[]){
        Memory newMem = new Memory();
        newMem.put("Dan", "30");
        System.out.println(newMem.get("Dan"));
    }

And the output was "30"

What does your code look like for creating/putting/getting?

I'm having the user type in put dan 30 or get dan into the command line http://pastie.org/912326
Raptrex
I figured it out, I created the object inside a while loop which doesnt work.
Raptrex
A: 
while(true)
{
    Scanner kb = new Scanner(System.in);
    Memory m = new Memory();
...

Every time you loop, you are creating a new Memory object instead of updating a reference to an existing one.

JRL