当前位置: 代码迷 >> Java相关 >> 帮我查查我的程序逻辑哪里出错了
  详细解决方案

帮我查查我的程序逻辑哪里出错了

热度:378   发布时间:2010-04-13 13:05:12.0
帮我查查我的程序逻辑哪里出错了
这个程序是要统计人的名字,和他们的编号, 用户数如 “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 java.io.*;
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 编辑 ]
搜索更多相关的解决方案: 程序逻辑  

----------------解决方案--------------------------------------------------------
数组越界问题,改用ArrayList就不用考虑数组的大小了
----------------解决方案--------------------------------------------------------
规定我们不可以用ArrayList
----------------解决方案--------------------------------------------------------
以下是引用suckdog在2010-4-14 10:23:39的发言:

规定我们不可以用ArrayList
那你就将数组设大些
----------------解决方案--------------------------------------------------------
要求我们先设定大小为10,然后需要大了再增加, 他就是为了考我们会不会增加, 不过现在问题出在我输入 "put john 99" 时数据不能够存进去, 出现arrayOutOfBound exception 错误, 所以大家再帮我看看哪里不对了
----------------解决方案--------------------------------------------------------
程序中最大的错误是字符串判断是否相等不能用“==”,要用equals()
----------------解决方案--------------------------------------------------------
把array的对比的方法改过后, 现在又是NullPointerException 错误了, 又是哪里的问题呢?
----------------解决方案--------------------------------------------------------
public class Person {
    private String name;
    private int number;
    protected String getName() {
        return name;
    }
    protected void setName(String name) {
        this.name = name;
    }
    protected int getNumber() {
        return number;
    }
    protected void setNumber(int number) {
        this.number = number;
    }
}


public class Memory {
    private final int STR_SIZE = 9;
    private final  int SIZE_INCREASE = 9;
    private int  size = 0;
    private Person[] persons = new Person[STR_SIZE];

    public Memory() {
        
    }
   
    private Person[] increaseStr(Person[] str, int increaseSize){
        Person[] result = new Person[str.length+increaseSize];
        for(int i=0;i<str.length;i++){
            result[i]=str[i];
        }
        return result;
    }

    public void put(String name, String numberOne) {
        if(size>=persons.length){
            persons = increaseStr(persons,SIZE_INCREASE);
            System.out.println(persons.length);
        }
        System.out.println("sdf"+persons.length);
        Person p=new Person();
        p.setName(name);
        p.setNumber(Integer.parseInt(numberOne));
        persons[size] = p;
        size++;
    }

    public String get(String name) {
        int result=-1;
        for(int i=0;i<size;i++){
            if(persons[i].getName().equals(name)){
                result=persons[i].getNumber();
                break;
            }
        }
        return new Integer(result).toString();
    }
   
    public int getSize(){
        return size;
    }

    public void remove(String name) {
        int j=0,i;
        for(i=0;i<size;i++){
            if(!persons[i].getName().equals(name)){
                persons[j++] = persons[i];
            }else{
               
            }
        }
        if(i==j)
            return;
        persons[size-1]=null;
        size--;
    }

    public void clear() {
        for (int i = 0; i < size; i++) {
            persons[i] = null;
        }
        size=0;
    }
}
----------------解决方案--------------------------------------------------------
谢谢你写得程序,不过所有的指令, 如put..., get...., size, remove, clear, 这些必须在memory class里写, 所以说困难的点, 大家再在我的程序上找找错误
----------------解决方案--------------------------------------------------------
你写的程序,逻辑本身就错误的,就算改得好,也是一个很差的程序。
我写的程序,都跟你的指令一样的,只要把
public int getSize(){
    return size;
}
改成
public int size(){
   return size;
}
就行了。

还有你的测试函数(Hw03)中的
String[] array=new String[2];
数组长度不够,输入remove的时候就溢出了,所以改成
String[] array=new String[5];
就可以了。

----------------解决方案--------------------------------------------------------