【问题标题】:addPerson (adds to an array) method isnt working, what's wrong?addPerson(添加到数组)方法不起作用,怎么了?
【发布时间】:2016-03-16 21:45:05
【问题描述】:

我是一名从事项目的学生,我有一个方法接受一个人,将其添加到数组 (Admits[]) 并将数组写入文件。该方法当前不执行此操作,因为 numSoFar 值不会增加并且人员不会保存到数组中。 (或者我在我的测试类中犯了一个错误)我把这两个类的编码都放在下面,希望有人能指出我做错了什么的方向。由于这是一个学校项目,请尽量不要太具体,只是告诉我一些关于为什么我的编码可能不起作用的建议。我不想把别人的工作当作我自己的;我只需要一双新的眼睛来看看我没有的东西。

在我的数据库类中编码

    //adds a Person to the Database
public void addPerson(Person admit)
{
    for(int i = 0; i < numSoFar; i++)//i = -1 when admit's alpabetical spot in Database has been located
    {
        if((i == numSoFar-1) || (i == numSoFar))//there is no Person in this index, so this is the end of the database
        {
            Admits[i] = admit;
            numSoFar = numSoFar + 1;
        }
        else
        {
            if(Admits[i].getLN().compareTo(admit.getLN()) == -1)//last name comes before last name of Person at index i
            {
                Person current= Admits[i];
                Admits[i] = admit;
                while(i <= numSoFar)
                {
                    Person next = Admits[i+1];
                    Admits[i+1] = current;
                    current = next;
                    i++;
                }
                numSoFar++;
            }
            else
            {
                if(Admits[i].getLN().equalsIgnoreCase(admit.getLN()))//admit and Person at index i have the same last name
                {
                    if(Admits[i].getFN().compareTo(admit.getFN()) == -1)
                    {
                        Person current= Admits[i];
                        Admits[i] = admit;
                        while(i < numSoFar)
                        {
                            Person next = Admits[i+1];
                            Admits[i+1] = current;
                            current = next;
                            i++;
                        }
                        numSoFar++;
                    }
                    else
                    {
                        if(Admits[i].getFN().equalsIgnoreCase(admit.getFN())) //admit and Person at index i are the same person
                        {
                            Scanner in = new Scanner(System.in);
                            System.out.println("There is already a person with this name in the database.");
                            int c = 0;
                            while(c != 1 || c != 2)
                            {
                                System.out.println("If you would like to keep that person, enter '1', and if you would like to replace him/her with the person entered, enter 2.");
                                if(c == 2)
                                {
                                    Admits[i] = admit;  
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    this.writeToFile();
}

这是在我的测试人员中添加新人的代码(我在案例 1 后停止

    Person[] Admits = new Person[5000];
    Database dB = new Database(Admits);
    dB.fillFromFile();//fills array with info from text file
    Scanner in = new Scanner(System.in);
    String c = "0";
    System.out.println("Hello, and thank you for using the Notre Dame admitted students friend-finder");
    while(!c.equals("6")) //Menu for user to traverse, is exited when user enters a 6
    {
        System.out.println("Please pick the desired action from one of the options below and enter its number.");
        System.out.println("  1: Enter info for a new admitted student and check matches");
        System.out.println("  2: Change info for an admitted student already in the database");
        System.out.println("  3: Delete an admitted student from the database");
        System.out.println("  4: Log in as an admitted student to check matches");
        System.out.println("  5: View contact info for a certain person in the database");
        System.out.println("  6: Exit the program");
        c = in.next();
        switch(c)
        {
            case "1": //create new student and check matches
            System.out.println("Enter your first name:");
            String firstName = in.next();
            System.out.println("Enter your last name:");
            String lastName = in.next();
            System.out.println("Enter your gender:");
            String gen = in.next();
            Person p = new Person(lastName, firstName, gen);

            //String chracteristics
            System.out.println("Are you an a. introvert or b. extrovert? (enter a or b):");
            String traitIntroExtro = in.next();
            while(!(traitIntroExtro.equalsIgnoreCase("a") || traitIntroExtro.equalsIgnoreCase("b")))
            {
                System.out.println("Invalid choice.Please re-enter your choice:");
                traitIntroExtro = in.next();
            }
            if(traitIntroExtro.equalsIgnoreCase("a"))
            {
                p.setTraitIntroExtro("Introvert");
            }
            else
            {
                p.setTraitIntroExtro("Extrovert");
            }

上面的编码基本上是用不同的变量重复的,但是因为有很多我会切到案例1的末尾

    //facebook url to contact matches with
            System.out.println("Please enter the url of your facebook profile page:");
            String url = in.next();
            p.setFacebookUrl(url);
            dB.addPerson(p);
            p.fillMatches(dB);
            boolean first = dB.first();
            if(first == true)//the database only has one person in it
            {
                System.out.println("You are currently the only person in the database.");
            }
            else//the database has atleast one person in it
            {
                System.out.println("Your top 2 most compatible people currently in the data base are:");
                System.out.println(p.getMatches().getHead().getPerson() + ", who can be found at " + p.getMatches().getHead().getPerson().getFacebookUrl());
                if(dB.getNumSoFar() == 2)
                {
                    System.out.println("This is the only other person in the database.");
                }
                else
                {
                    System.out.println(p.getMatches().getHead().getNextNode().getPerson() + ", who can be found at " + p.getMatches().getHead().getNextNode().getPerson().getFacebookUrl());
                }

            }
            break;

【问题讨论】:

  • 请发布addToDatabase 方法的代码,如果这样不起作用。
  • 那是 addPerson。很抱歉没有澄清。
  • 我可以建议你尝试使用else if而不是else { if,这样你就不会得到这么深的缩进代码。
  • Admits[i].getLN().compareTo(admit.getLN()) == -1 compareTo 接口实际上并不要求在第一个小于第二个的情况下返回 -1 - 只是返回值为负。因此,您实际上应该在这里查看&lt; 0,而不是== -1
  • 是的!我只是把我们学到的所有东西都格式化了,但这会让它不那么碍眼

标签: java arrays object insert writetofile


【解决方案1】:

让我们看一下您将 Person 添加到数组末尾的情况(就像还没有条目时一样):

for(int i = 0; i < numSoFar; i++)
{
    if((i == numSoFar-1) || (i == numSoFar))
    {
        Admits[i] = admit;
        numSoFar = numSoFar + 1;
    }
    else
    { /* doesn't matter */ }
}

你的for循环永远不会退出:如果i = numSoFar-1,那么for循环的每次迭代都会将inumSoFar都加1,因此i &lt; numSoFar将永远是true

另外,一条评论提到i 将是-1。除了通过减速和递增之外,我没有看到您分配i 的任何地方,那么它怎么可能是-1?

最后,一旦你有了一个工作版本,我建议你在Code Review Stack Exchange 上发布你的代码。我会在 Stack Overflow 上指出一些超出范围的非功能性问题(例如 cmets 中提到的 else { if (...) {...} }else if (...) {...})。

【讨论】:

  • 是的...修复了它,但它仍然以某种方式超越了该声明。这几乎就像测试人员无法访问数据库类
  • 您的系统发生了太多事情,我无法准确地帮助您,但我可以给您一些调试建议:创建一个不涉及(显然很复杂)输入的测试用例人员数据:只需将一到三个预构建的人员添加到数据库中,看看会发生什么。将调试打印语句放在它应该采用的逻辑路径上,看看程序是否在做你期望它做的事情。通过此过程,您应该能够找出问题或创建问题的较小 MCVE 以在此处提供。
  • 是的,来自测试仪和数据库的东西不会相互通信。这就是问题所在......虽然不知道如何解决它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多