-1

I'm a student working on a project, and I have a method takes a Person, adds it to the array (Admits[]) and writes the array to file. The method currently does not do this, as the numSoFar value does not increase and the person is not saved to the array. (either that or i made a mistake in my tester class) I put coding of both classes below, and hope someone can point me in the direction of what i am doing wrong. As this is a school project please try not to be too specific actually, just tell me some suggestions as to why my coding may not work. I don't want to pass off someone else's work as my own; i just need a new set of eyes to see what I haven't.

Coding in my Database class

    //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();
}

This is the coding to add a new person in my tester (i stopped after case 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");
            }

There above coding is basically repeated with different variables, but as there are many i will cut to the end of case 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;
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Please post the code of the `addToDatabase` method, if that's what isn't working. – Andy Turner Mar 16 '16 at 21:47
  • that's addPerson. Sorry for not clarifying. – Brenna Mar 16 '16 at 21:49
  • 1
    Can I suggest that you try using `else if` instead of `else { if`, so that you don't get such deeply indented code. – Andy Turner Mar 16 '16 at 21:49
  • `Admits[i].getLN().compareTo(admit.getLN()) == -1` The `compareTo` interface doesn't actually require that -1 is returned if the first thing is less than the second - only that the return value is negative. As such, you should actually check `< 0` here, not `== -1`. – Andy Turner Mar 16 '16 at 21:52
  • yup! I'm just formatting everything how we learned it but that will def make it less of an eyesore – Brenna Mar 16 '16 at 21:53
  • (This is the documentation of [`Comparable.compareTo`](https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)), if you're wondering where it says this). – Andy Turner Mar 16 '16 at 21:53
  • thanks! ill fix that. – Brenna Mar 16 '16 at 21:56
  • Andy: I think is a rather bad suggestion. I recommend to **always** use braces on your blocks; even when talking about one line then/else statements. The key is not to use less braces, the key is to dramatically increase the number of methods; so that each method only has a few lines. That makes your code readable! (see Robert Marting; "Clean code" ) – GhostCat Mar 16 '16 at 21:57
  • When I'm adding my first person, that shouldn't be an issue even though, right? That's where I've been having the issue. (although I have now fixed a problem I didn't know i had as well so thankyou) – Brenna Mar 16 '16 at 21:58
  • @Jägermeister I am not advocating omitting braces. I am just saying don't use `} else { if (...) { } }` because `} else if (...) { }` leads to less indented code, which is easier to read. – Andy Turner Mar 16 '16 at 21:58
  • I think I was able to parse the heart of the issue, but please, provide a [MCVE](http://stackoverflow.com/help/mcve) in the future, and consider editing this question to provide one as well. The amount of code you have posted makes it difficult for answerers to understand the issue, and as such, help you. – CAD97 Mar 16 '16 at 22:09
  • Andy: I got that. And I am telling you: your suggestion is the wrong approach. You buy slightly improved readability for increased risk getting "wrong blocks". There are good reasons why most style guides tell you to **never omit** braces. Again: there are better ways to achieve (more) readability; without that additional risk. – GhostCat Mar 16 '16 at 22:29

1 Answers1

0

Let's take a look at the case you're adding a Person to the end of the array (as you would if there were no entries yet):

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

Your for loop is never going to exit: if i = numSoFar-1, then each iteration of the for loop will increment both i and numSoFar by 1 and as such i < numSoFar will always be true.

Also, a comment mentions that i will be -1. I don't see any place where you assign i other than by deceleration and increment, so how could it ever be -1?

Finally, once you've got a working version I'd suggest posting your code over at the Code Review Stack Exchange. There are some non-functional issues out of scope for here on Stack Overflow that I'd point out there (such as the else { if (...) {...} } vs else if (...) {...} mentioned in the comments).

Community
  • 1
  • 1
CAD97
  • 1,160
  • 12
  • 29
  • yeah...fixed that and it still somehow gets past that statement. Its almost like the tester has no access to the Database class – Brenna Mar 17 '16 at 02:13
  • 1
    There's too much going on with your system for me to be able to accurately help you, but I can give you some debug advise: create a test case that doesn't involve the (obviously complicated) entering of Person data: just add one to three prebuilt Persons to the database and see what happens. Put debug print statements on the path of logic it should be taking and see if the program is doing what you expect it to do. Through this procedure you should either be able to figure out the issue or create a smaller MCVE of the issue to provide here. – CAD97 Mar 17 '16 at 02:24
  • yup, things from tester and Database wont communicate with eachother. thats where the issue is...dont know how to solve it though – Brenna Mar 17 '16 at 03:48