Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java

Name: Anonymous 2010-03-01 7:31

word1 = scan.next();
word2 = scan.next();
word3 = scan.next();
Person [] p = personlist.getPersons();

if (word1.equals("add")) {
    personlist.addPerson(word2,word3);
}
if (word1.equals("show")) {
    for (int i = 0; i < p.length; i++) {
        if (word2.equals(p[i].name)) {
        System.out.println("Navn " + p[i].name);
        System.out.println("Nummer " + p[i].phone);
        System.out.println("Antall venner: " + p[i].friendList.getPersons().length);
        }
    }
/*System.out.println("Navn " + personlist.getPerson(word2).name);
System.out.println("Nummer " + personlist.getPerson(word2).phone);
System.out.println("Antall venner " + personlist.getPerson(word2).friendList.getPersons().length);
*/
}

Why did I have to comment out the last part and use the p array loop. The getPerson() does work, just not in this case, any idea?

Name: Anonymous 2010-03-01 11:15

>>1
You might want to wrap that commented-out block in a try { ... } catch(NullPointerException e) { } statement.  word1 may not always be "add," in which case you're searching personlist for a word2 that doesn't exist, but proceeding forward with it as if it did exist.  I don't know what the base case of getPerson(String) returns but I'm assuming null if it finds nothing.

If you're going to run with that for loop anyway, unless you allow redundant entries in personlist, break after you find the right person and have outputted their information.

If you're not going to include pertinent source code, methods, etc., do properly explain what it does and what it returns.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List