Results 1 to 10 of 10
- 05-26-2010, 08:36 PM #1
Member
- Join Date
- May 2010
- Location
- Muskoka, Ontario
- Posts
- 3
- Rep Power
- 0
Java Phonebook With Arrays // HELP! //
Hi all,
having a bit of trouble, could use some pointers.
ASSIGNMENT:
Write an application that allows a user to enter the names and phone numbers of up to 20 friends. Continue to prompt the user for names and phone numbers until the user enters "zzz" or has entered 20 names, which ever comes first. When the user is finished entering names, produce a count of how many names were entered, but make certain to count the application-ending dummy "zzz" entry. Then display the names. Ask the user to type one of the names and display the corresponding phone number. Save the application as phonebook.java
Here is the code I have written so far:
Java Code:import java.util.Scanner; import java.util.ArrayList; public class phonebook { int x; final int maxContacts = 3; double[] pNums = new double[4]; String[] pNames = new String[4]; String nameSearch; boolean nameWasFound = true; Scanner keyboard = new Scanner(System.in); public void getInfo() { for(x = 0; x < maxContacts; ++x) { System.out.println("Please enter a name "); pNames[x] = keyboard.next(); if(nameWasFound) { System.out.println("Please enter their phone number "); pNums[x] = keyboard.nextDouble(); } } System.out.println(""+pNames[x]); //I want to display all the names here, comes back null. System.out.println("Please enter a name to find "); // no idea how to search a name and display the corresponding number! nameSearch = keyboard.next(); if(nameSearch.equals(pNames[x])) { System.out.println("The name is"+pNames[x]); } } public static void main(String args[]) { phonebook show = new phonebook(); show.getInfo(); } }
- 05-26-2010, 08:53 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I'd suggest breaking the problem apart a bit more. You have a Phonebook class (btw, class names should start with an Upper case letter), why not make a Contact class, that contains the name and phone number? That way, you don't have to fiddle around with two arrays. Also, your assignment states, that you should have 20 entries, but you initialize your arrays to 4 elements, and you only allow 3 entries. You also don't check if the user entered "zzz". Next:
Why a double? I may be out of touch with cutting-edge technology, but I've never seen a phonenumber like 3.14159.Java Code:pNums[x] = keyboard.nextDouble();
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-26-2010, 08:56 PM #3
Are you getting errors compiling? Please post the error message.
If execution errors, post message here.
You need to design the program before writing it. Break the program up into parts and describe what each part is supposed to do.
Comments on the code:
Class names begin with uppercase letters.
After getting the names and phone numbers, shouldn't there be code to prompt the user for the name to lookup?
Why 3 for max and 4 for size of arrays? What happened to the 20 max?
Where is the "zzz" end of input flag defined/used?
What is the nameWasFound variable used for?
- 05-26-2010, 10:01 PM #4
- 05-26-2010, 10:16 PM #5
i made some small corrections to your code:
Java Code:import java.util.Scanner; import java.util.ArrayList; public class phonebook { int x; final int maxContacts = 3; String[] pNums = new String[3]; String[] pNames = new String[3]; String nameSearch; boolean nameWasFound = true; Scanner keyboard = new Scanner(System.in); public void getInfo() { for (x = 0; x < maxContacts; ++x) { System.out.print("Please enter a name: "); pNames[x] = keyboard.next(); if (nameWasFound) { System.out.print("Please enter their phone number: "); pNums[x] = keyboard.next(); } } System.out.println("\nList of names and phone number"); System.out.println("------------------------------"); for (int i = 0; i < pNames.length; i++) { System.out.println("" + pNames[i] + " phone: " + pNums[i]); // I want to display all the // names here, comes back null. } System.out.print("\n\nPlease enter a name to find "); // no idea how to // search a name and // display the // corresponding // number! nameSearch = keyboard.next(); for (int i = 0; i < pNames.length; i++) { if (nameSearch.equals(pNames[i])) { System.out.println("The name " + pNames[i] + " was found " + "with the phone number " + pNums[i]); } } } public static void main(String args[]) { phonebook show = new phonebook(); show.getInfo(); } }
and the output is
happy?Please enter a name: peter
Please enter their phone number: 1111
Please enter a name: sue
Please enter their phone number: 2222
Please enter a name: lisa
Please enter their phone number: 3333
List of names and phone number
------------------------------
peter phone: 1111
sue phone: 2222
lisa phone: 3333
Please enter a name to find sue
The name sue was found with the phone number 2222
- 05-26-2010, 10:53 PM #6
Member
- Join Date
- May 2010
- Location
- Muskoka, Ontario
- Posts
- 3
- Rep Power
- 0
Thank you!
Wow that is amazing. Makes total sense now that I see the completed code! Thanks so much! :D
- 05-26-2010, 11:33 PM #7
Poor OP. he'll never learn programming using copy and paste.
You only learn by making the mistakes and figuring out why and how to fix them.
- 05-26-2010, 11:46 PM #8
i wrote a class PhoneBook with methods addPerson, findPerson, writePhoneBook and so on. save the following code as PhoneBook
Java Code:import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; public class PhoneBook implements Serializable { String name; public void setName(String name) { this.name = name; } ArrayList<Person> pb = new ArrayList<Person>(); public void addPerson(Person p) { pb.add(p); } public void listPhoneBook() { System.out.println("\nListing of " + this.name); Iterator it = this.pb.iterator(); while (it.hasNext()) { System.out.println(it.next().toString()); } } public String findPerson(String name) { Iterator it = this.pb.iterator(); while (it.hasNext()) { Person p = (Person) it.next(); if (p.getName().equals(name)) { return p.getPhoneNumber(); } } return null; } public void writePhoneBook(PhoneBook p) { File file = new File("phonebook.java"); try { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(file)); out.writeObject(p); out.flush(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } public PhoneBook readPhoneBook(String file) { PhoneBook pb = null; try { ObjectInputStream in = new ObjectInputStream( new BufferedInputStream(new FileInputStream(file))); try { Object obj = in.readObject(); if (obj instanceof PhoneBook) { pb = (PhoneBook) obj; } } finally { in.close(); } } catch (Exception ex) { ex.printStackTrace(); } return pb; } } class Person implements Serializable { String name; String phoneNumber; public Person(String name, String phoneNumber) { this.name = name; this.phoneNumber = phoneNumber; } public String toString() { return name + ", phone: " + phoneNumber; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } }
the data you enter are saved in a the object Person and then added to the ArrayList, both defined in the class above.
now the main method looks a little different, but you can enter as much persons and numbers you want and when you have finished enter end as name and then all data you have entered are saved in a file phonebook.java (note: without no path). then to demostrate that the datas was saved a new phonebook is instantiated and the name sue is searched. save the code as PhoneBookExample
Java Code:import java.util.Scanner; public class PhoneBookExample { Scanner keyboard = new Scanner(System.in); PhoneBook MyPhoneBook ; public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); PhoneBook MyPhoneBook = new PhoneBook(); MyPhoneBook.setName("Private PhoneBook"); while (true){ // when you have entered all persons and // their numbers, exit the loop by entering // end and name System.out.print("Please enter a name: "); String name = keyboard.next(); if (name.equals("end")) { break; } System.out.print("Please enter their phone number: "); String phonenumber = keyboard.next(); Person p = new Person(name, phonenumber); MyPhoneBook.addPerson(p); } MyPhoneBook.listPhoneBook(); MyPhoneBook.writePhoneBook(MyPhoneBook); PhoneBook newPhoneBook = new PhoneBook(); String file = "phonebook.java"; newPhoneBook = newPhoneBook.readPhoneBook(file); newPhoneBook.setName("Readed serialized PhoneBook"); newPhoneBook.listPhoneBook(); String s; if ((s=newPhoneBook.findPerson("sue")) != null) { System.out.println("sue was found with phone number: " + s); } } }
when i run it i get this output:
Please enter a name: peter
Please enter their phone number: 0440-34598
Please enter a name: sue
Please enter their phone number: 0987/12345
Please enter a name: end
Listing of Private PhoneBook
peter, phone: 0440-34598
sue, phone: 0987/12345
Listing of Readed serialized PhoneBook
peter, phone: 0440-34598
sue, phone: 0987/12345
sue was found with phone number: 0987/12345
note, that the "Listing of Readed serialized PhoneBook" is readed from the file. you can surely enhance and adapt the code corresponding to your requirements. if you have question, feel free to ask. have fun.
- 05-27-2010, 05:32 AM #9
well I think you're right in a way, but i also think you're wrong in a way too.(well, not wrong but perhaps you're assuming that all noobs to the forum are only posting to get a quick fix)
When someone posts a genuine attempt at a solution, if someone with more experience can see that OP has missed something, or is going in completely the wrong direction, i think it can save the OP a good deal of frustration if they can be shown a complete and /or more effective solution.
People don't tend to learn much when they are frustrated. and are likely to learn nothing if they feel they are being led by the nose. People are more likely to learn if they trust that the participants in the learning environment are genuinely trying to help.
It's a tough call to make and all of the more experienced members in this forum have their own subtly different ways of helping. that's what makes this forum interesting. Moreover, the experienced members usually have a knack of being able to determine which noobs are genuinely interested and which noobs are looking for a quick fix and dealing with such noobs accordingly.
it is of course a totally different matter where an OP says "Here is my assignment, please reveal how i do it" (and this is where you're right)
The less experienced amongst us (myself included) will still try to help because we ourselves learn from the experience of trying to help others.
in conclusion:
when you see
you can bet (Odds on). that OP has not only gained but will be back again to learn more and ultimately contribute to the best java forum there is.:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
- 10-04-2011, 03:41 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Re: Java Phonebook With Arrays // HELP! //
Im having major problems with java. I have to a program that reads name and numbers into two arrays of Strings. Then the program will print out the two arrays in a meaningful way. Then the program will set up a loop and inside the loop, the program will print out a menu as follow.
1. Check a name
2. Check a number
3. Change a name
4. Change a number
5. Add a name and a number
6. Print all names and numbers
7. Quit
Please if someone could HELP ME!!!!!!!
Similar Threads
-
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
java arrays
By miko5054 in forum New To JavaReplies: 15Last Post: 03-18-2010, 05:17 PM -
phonebook update
By nanna in forum New To JavaReplies: 5Last Post: 03-09-2009, 10:13 PM -
arrays in java
By mayhewj7 in forum New To JavaReplies: 4Last Post: 12-17-2008, 03:48 AM -
Arrays in Java
By hiranya in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks