| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1008 人关注过本帖
标题:帮我查查我的程序逻辑哪里出错了
取消只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
已结贴  问题点数:10 回复次数:7 
帮我查查我的程序逻辑哪里出错了
这个程序是要统计人的名字,和他们的编号, 用户数如 “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 编辑 ]
搜索更多相关主题的帖子: 程序逻辑 
2010-04-13 13:05
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
规定我们不可以用ArrayList
2010-04-14 10:23
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
要求我们先设定大小为10,然后需要大了再增加, 他就是为了考我们会不会增加, 不过现在问题出在我输入 "put john 99" 时数据不能够存进去, 出现arrayOutOfBound exception 错误, 所以大家再帮我看看哪里不对了
2010-04-14 12:28
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
把array的对比的方法改过后, 现在又是NullPointerException 错误了, 又是哪里的问题呢?
2010-04-14 14:27
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
谢谢你写得程序,不过所有的指令, 如put..., get...., size, remove, clear, 这些必须在memory class里写, 所以说困难的点, 大家再在我的程序上找找错误
2010-04-14 23:28
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
以下是引用linjx0123在2010-4-15 09:30:06的发言:

你写的程序,逻辑本身就错误的,就算改得好,也是一个很差的程序。
我写的程序,都跟你的指令一样的,只要把
public int getSize(){
    return size;
}
改成
public int size(){
   return size;
}
就行了。

还有你的测试函数(Hw03)中的
String[] array=new String[2];
数组长度不够,输入remove的时候就溢出了,所以改成
String[] array=new String[5];
就可以了。
我的不是character array, 我的是string array,所以不会溢出来, 上面的一个method,改成你那样其实本质没区别, 望大家再来帮我找找错误
2010-04-15 10:16
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
你的程序我测试过了, 不过每次我输入6个字母的名字就当掉了, 如put andrew 77

[ 本帖最后由 suckdog 于 2010-4-15 11:31 编辑 ]
2010-04-15 11:28
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
我输入一个不存在的人, 如果"get john", 程序给出“that key returns a -1", 我程序里不是写得return null吗, 为什么会给我-1?
2010-04-15 12:15
快速回复:帮我查查我的程序逻辑哪里出错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023048 second(s), 8 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved