Results 1 to 7 of 7
- 03-19-2013, 10:50 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Re: Java Phonebook With Arrays // HELP! //
I have the same excercise but different problem hope you guys can help.
Here's my code:
import java.util.*;
public class PhoneBook {
public static void main(String[] args) {
//Initializing scanner array 'friends' ,'phone' and variable.
Scanner sc = new Scanner(System.in);
String[] friends = new String[20];
String[] phone = new String[20];
String name;
String entry = "zzz";
int count = 0;
//Prompting user to input Array elements.
do{
System.out.println("Enter a friends name:");
name = sc.nextLine();
if(name.equalsIgnoreCase(entry)){
System.out.println("Thank you.");
}
else{
friends[count] = name;
System.out.println("Enter your friends phone number (XXX-XXX-XXXX)");
phone[count] = sc.nextLine();
}
count++;
}while(name.equalsIgnoreCase("zzz") || count <= 19);
}
}
I'm stuck at the while command which wont recognize the "zzz"
- 03-19-2013, 11:18 PM #2
Re: Java Phonebook With Arrays // HELP! //
Post moved to its own thread.
Add a println() to print out the value of name and count just before the while statement so you can see what the computer sees when the code executes.while command which wont recognize the "zzz"
You do release what an OR operator will do? If either of the expressions is true, the while will loop.
I'd guess that it will loop about 19 timesIf you don't understand my response, don't ignore it, ask a question.
- 03-19-2013, 11:23 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Re: Java Phonebook With Arrays // HELP! //
I have the same exercise but different problem. I'm stuck in a while loop which wont recognize when "Zzz" is entered and the program thinks its a name can someone help me out?
Here's the code:
Java Code:import java.util.*; public class PhoneBook { public static void main(String[] args) { //Initializing scanner array 'friends' ,'phone' and variable. Scanner sc = new Scanner(System.in); String[] friends = new String[20]; String[] phone = new String[20]; String name; String entry = "zzz"; int count = 0; //Prompting user to input Array elements. do{ System.out.println("Enter a friends name:"); name = sc.nextLine(); if(name.equalsIgnoreCase(entry)){ System.out.println("Thank you."); } else{ friends[count] = name; System.out.println("Enter your friends phone number (XXX-XXX-XXXX)"); phone[count] = sc.nextLine(); } count++; }while(name.equalsIgnoreCase(entry) || count <= 19); System.out.println("The number of friends you entered is: "+count); System.out.println(""); } }
- 03-20-2013, 01:39 AM #4
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Java Phonebook With Arrays // HELP! //
while(name.equalsIgnoreCase(entry) || count <= 19)
WHILE count < 20 OR name = zzz THEN count += 1 and Keep Going
isn't really what you meant? I'm guessing WHILE count < 20 and name NOT zzz ?
- 03-22-2013, 12:30 AM #5
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Re: Java Phonebook With Arrays // HELP! //
Finished it thank you for responding though, ended up with for loops but I imagine if I did this it work also
while loop section of previous code:
Java Code:do{ System.out.println("Enter a friends name:"); name = sc.nextLine(); if(name.equalsIgnoreCase("Zzz"))break; else{ friends[count] = name; System.out.println("Enter your friends phone number (XXX-XXX-XXXX)"); phone[count] = sc.nextLine(); } count++; }while(count <= 19);
Completed Code
Java Code:import java.util.*; public class PhoneBook { public static void main(String[] args) { //Initializing scanner array 'friends' ,'phone' and variable. Scanner sc = new Scanner(System.in); String[] friends = new String[20]; String[] phone = new String[20]; String name; String nameAns; int count = 0; //Prompting user to input Array elements. for (int i = 0; i <= 19; i++) { System.out.println("Enter a friends name:"); name = sc.nextLine(); //If user enters "Zzz" automatically breaks for loop if(name.equalsIgnoreCase("zzz"))break; else{ friends[i] = name; System.out.println("Enter your friends phone number (XXX-XXX-XXXX)"); phone[i] = sc.nextLine(); count ++; } } //Displays amount of friends typed in System.out.println("\nThe number of friends you entered is: "+count); //Displays the names typed in System.out.println("The names you entered are:"); for (int i = 0; i <= 19; i++) { //When an element of the array equals null automatically breaks the for loop if(friends[i]== null)break; else { //Prints the names System.out.println(friends[i]); } } System.out.println("Enter one of the names:"); nameAns = sc.nextLine(); //Quick scan of all the elements for (int i = 0; i <= 19; i++){ //If the variable nameAns equals one of the elements in the array quickly prints //it out and the number corresponding to it. if (nameAns.equalsIgnoreCase(friends[i])){ System.out.println(friends[i]+": "+phone[i]); } } } }
- 03-23-2013, 02:13 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 24
- Rep Power
- 0
Re: Java Phonebook With Arrays // HELP! //
I am a little curious as to why you are using i<= 19 instead of < array.length and so on?
- 03-26-2013, 04:21 AM #7
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Need help with Phonebook program
By evvdogg in forum New To JavaReplies: 11Last Post: 12-07-2012, 04:05 AM -
Help! Phonebook.java Project
By javahelp1234 in forum New To JavaReplies: 1Last Post: 03-30-2012, 01:43 AM -
Phonebook HashMap Example
By forms in forum New To JavaReplies: 3Last Post: 01-23-2012, 07:09 PM -
Java Phonebook With Arrays // HELP! //
By K-Scale in forum New To JavaReplies: 9Last Post: 10-04-2011, 03:41 AM -
phonebook update
By nanna in forum New To JavaReplies: 5Last Post: 03-09-2009, 10:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks