Value isnt being saved in the strings
Posted
by Raptrex
on Stack Overflow
See other posts from Stack Overflow
or by Raptrex
Published on 2010-04-09T22:59:10Z
Indexed on
2010/04/09
23:03 UTC
Read the original article
Hit count: 273
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. Anyone know whats 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]);
}
}
}
© Stack Overflow or respective owner