Results 1 to 7 of 7
  1. #1
    Zoul787 is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default 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"

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Java Phonebook With Arrays // HELP! //

    Post moved to its own thread.

    while command which wont recognize the "zzz"
    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.

    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 times
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zoul787 is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

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

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default 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 ?

  5. #5
    Zoul787 is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default 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]);
        		}
        	}
        }
    }

  6. #6
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default 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?

  7. #7
    Zoul787 is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: Java Phonebook With Arrays // HELP! //

    I'm still new to this :)

Similar Threads

  1. Need help with Phonebook program
    By evvdogg in forum New To Java
    Replies: 11
    Last Post: 12-07-2012, 04:05 AM
  2. Help! Phonebook.java Project
    By javahelp1234 in forum New To Java
    Replies: 1
    Last Post: 03-30-2012, 01:43 AM
  3. Phonebook HashMap Example
    By forms in forum New To Java
    Replies: 3
    Last Post: 01-23-2012, 07:09 PM
  4. Java Phonebook With Arrays // HELP! //
    By K-Scale in forum New To Java
    Replies: 9
    Last Post: 10-04-2011, 03:41 AM
  5. phonebook update
    By nanna in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •