帮我查查我的程序逻辑哪里出错了
这个程序是要统计人的名字,和他们的编号, 用户数如 “put john 49" 我就要把他的名字 john和他的编号49记下来存在array里, 如果用户输入”get john" 那程序就要反回出这个人的代号49出来, 如果用户输入 “remove john”, 那这个人的档案就删除了, 如果输入size就把档案里的人数统计出来问题现在是为什么我每次输入put..., get....指令的时候都给我一个 "arrayOutOfBound exception" 呢? 我输入size, help的时候都没问题
public class Memory
{
public int size=9;
private String command; // To get a command
String[] a=new String[size];
String[] b=new String[size];
String[] extended = new String[1000000];
String[] extendedOne = new String[1000000];
private String name;
private String number;
public Memory()
{
}
public int size()
{
int i=0, count=0;
while (extended[i]=="")
{
count++;
i++;
}
if (count==0)
return count;
else
return count+1;
}
public void put (String nameOne, String numberOne)
{
int i=0, flag=0;
do
{
if(a[i]==nameOne)
{
a[i]=nameOne;
b[i]=numberOne;
flag=1;
}
else if (a[i]!=nameOne && a[i]!="" && i>size)
{
extended[i] = nameOne;
extendedOne[i] = numberOne;
System.arraycopy(a, 0, extended, 0, a.length);
System.arraycopy(b, 0, extendedOne, 0, b.length);
flag=1;
}
else if(a[i]!=nameOne && a[i]!="")
i++;
else if(a[i]=="")
{
a[i]=nameOne;
b[i]=numberOne;
flag=1;
}
extended[i]=a[i];
extendedOne[i]=b[i];
}while(flag!=1);
}
public String get (String nameOne)
{
int i=0, element;
boolean found;
element=-1;
found=false;
do
{
if (a[i]==nameOne)
{
found=true;
element=i;
}
i++;
}while (i<a.length);
if(found=true)
return b[element];
else
{
b[element]="-1";
return b[element];
}
}
public void remove (String nameOne)
{
int i=0;
while (i<a.length)
{
if(a[i]==nameOne)
{
a[i]=a[i+1];
b[i]=b[i+1];
}
i++;
}
}
public void clear()
{
for(int i=0; i<a.length; i++)
{
a[i]="";
b[i]="";
}
}
}
import java.util.Scanner;//Needed for the Scanner class
import *;
import java.util.StringTokenizer;
public class Hw03
{
public static void main(String[] args) throws IOException
{
int i=0, j=0, flag=0, index=9;
String command, name, number; // To hold command
String[] array=new String[2];
String[] arrayOne=new String[index];
String[] arrayTwo=new String[index];
Scanner keyboard=new Scanner (System.in);
do
{
System.out.print("Command? ");
command=keyboard.nextLine();
StringTokenizer strTokenizer = new StringTokenizer(command, "get put");
while (strTokenizer.hasMoreTokens())
{
array[i]=strTokenizer.nextToken();
i++;
}
i=0;
name=array[0];
number=array[1];
Memory mem = new Memory();
if(command.startsWith("size"))
System.out.println("Currently remembering "+mem.size()+" things.");
else if(command.startsWith("help"))
{
System.out.println("get Retrieve an item by its key");
System.out.println("put Store a <key,value> pair");
System.out.println("clear Removes all pairs from the memory");
System.out.println("exit Exit the program");
System.out.println("help Display this text");
System.out.println("remove Removes a given key from the memory");
System.out.println("size Report the size of the memory");
}
else if(command.startsWith("exit"))
flag=1;
else if(command.startsWith("put"))
mem.put(name, number);
else if(command.startsWith("get"))
{
if(mem.get(name)=="-1")
System.out.println("That key gets null.");
else
System.out.println("That key gets "+mem.get(name));
}
else if(command.startsWith("remove"))
mem.remove(name);
else if(command.startsWith("clear"))
{
mem.clear();
System.out.println("Memory has been cleared!");
}
}while (flag!=1);
}
}
[ 本帖最后由 suckdog 于 2010-4-14 03:11 编辑 ]