Trouble with Exercise 94

454 views
Skip to first unread message

Jonathan Middleton

unread,
Sep 15, 2014, 9:12:57 PM9/15/14
to moo...@googlegroups.com
I'm having some trouble with exercise 94.  All parts of the exercise pass except the last part (searching the phone book for a number given a name as a search parameter).  The link to my work is here: TMC pastebin of Exercise 94

The method that I'm having a problem with is public String searchNumber(String search) in the Phonebook class.  
The if(statement) won't accurately search for items in the string.  Search for "pe" or "kk" should return true when reading "Pekka Mikkola", but it returns false.  Also, return won't print to console.  

I hope the //comments are enough to explain my process.  What am I missing?

Any unrelated critiques are also welcome.  I'm new to this and would gladly take some constructive criticism.

Thank you for your help.

Joni Salmi

unread,
Sep 16, 2014, 7:39:20 AM9/16/14
to moo...@googlegroups.com

Hi,

You code is working almost right. Your problem is that you are using
 
String result = "";

Lets say that we loop thru two persons and the first one is who we are looking for.

Result variable first gets value of first persons phone number because that's what we were looking for.
Now we still loop to next person, oh noh, no match. Result variable is set to "number not known".

I suggest that you remove result variable and use two different return statements in your method.  

Best regards
Joni

Jonathan Middleton

unread,
Sep 16, 2014, 3:41:04 PM9/16/14
to moo...@googlegroups.com
Thank you!  My mistake is obvious now, but the program will still not print to console.


The program compiles and runs; when running the local test (Alt-F6), all tests pass.  The problem which I don't understand is that the console does not display the return statement.

Joni Salmi

unread,
Sep 16, 2014, 7:44:48 PM9/16/14
to
You need to use print command to print data.

Change phonebook.searchNumber("Pek"); to System.out.println(phonebook.searchNumber("Pek"));
Reply all
Reply to author
Forward
0 new messages