Results 1 to 6 of 6
  1. #1
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default PhoneBook Application

    This application is supposed to allow a user to enter the names an phone numbers of up to 20 friends, until the user enters "zzz" or 20 names. Then the console is supposed to display the names and have the user enter a name to get the phone number. Here's my code:

    import java.util.Scanner;

    public class PhoneBook {

    public static void main(String[] args) {
    String name;
    int phoneNumber;
    final int nameAmount=20;
    NamesNumbers[] names=new NamesNumbers[nameAmount];
    int a;
    int b;
    final int phoneNumberAmount=20;
    final String quit="zzz";
    Scanner keyboard=new Scanner(System.in);

    while(a < nameAmount && b != phoneNumberAmount) {

    if(name != quit) {

    for(b=0; b < nameAmount; ++b) {
    names[b]=new NamesNumbers();

    System.out.print("Enter name or zzz to quit: ");
    name=keyboard.nextLine();
    names[b].setName(name);

    for(a=0; a < phoneNumberAmount; ++a) {
    System.out.print("Enter phone number: ");
    phoneNumber=keyboard.nextInt();
    names[b].setPhoneNumber(a, phoneNumber);
    }
    }
    a++;
    }
    for(b=0; b < nameAmount; ++b) {
    System.out.println("\nPhone numbers of " + names[b].getName());
    for(a=0; a < phoneNumberAmount; ++a)
    System.out.print(names[b].getPhoneNumber(a) + " ");
    System.out.println();
    }
    }

    System.out.print("\n\nEnter a name to see their phone number: ");
    name=keyboard.nextLine();

    for(b=0; b < names.length; ++b)
    if(name.equals(names[b].getName()))
    for(a=0; a < phoneNumberAmount; ++a)
    System.out.print(names[a].getPhoneNumber(a) + " ");

    System.out.println();

    }
    }


    I can't seem to get it correct, every time I correct a compiler error, another set of them appear. Any help would be greatly appreciated!

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

    Default Re: PhoneBook Application

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    If you get compiler errors, copy the full text and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default Re: PhoneBook Application

    Java Code:
    import java.util.Scanner;
    
    public class PhoneBook	{
    
    	public static void main(String[] args)	{
    		String name;
    		int phoneNumber;
    		final int nameAmount=20;
    		NamesNumbers[] names=new NamesNumbers[nameAmount];
    		int a;
    		int b;
    		final int phoneNumberAmount=20;
    		final String quit="zzz";
    		Scanner keyboard=new Scanner(System.in);
    		
    		while(a < nameAmount && b != phoneNumberAmount)	{
    		
    			if(name != quit)	{
    		
    			for(b=0; b < nameAmount; ++b)	{
    				names[b]=new NamesNumbers();
    			
    			System.out.print("Enter name or zzz to quit: ");
    			name=keyboard.nextLine();
    			names[b].setName(name);
    		
    				for(a=0; a < phoneNumberAmount; ++a)	{
    					System.out.print("Enter phone number: ");
    					phoneNumber=keyboard.nextInt();
    					names[b].setPhoneNumber(a, phoneNumber);
    				}
    			}
    			a++;
    			}
    			for(b=0; b < nameAmount; ++b)	{
    			System.out.println("\nPhone numbers of " + names[b].getName());
    			for(a=0; a < phoneNumberAmount; ++a)
    				System.out.print(names[b].getPhoneNumber(a) + " ");
    			System.out.println();
    			}
    		}
    		
    		System.out.print("\n\nEnter a name to see their phone number: ");
    		name=keyboard.nextLine();
    		
    		for(b=0; b < names.length; ++b)
    			if(name.equals(names[b].getName()))
    				for(a=0; a < phoneNumberAmount; ++a)
    					System.out.print(names[a].getPhoneNumber(a) + " ");
    					
    		System.out.println();
    		
    	}
    }
    C:\Users\Justin\Java\Week08>javac PhoneBook.java
    PhoneBook.java:16: error: variable a might not have been initialized
    while(a < nameAmount && b != phoneNumberAmount) {
    ^
    PhoneBook.java:16: error: variable b might not have been initialized
    while(a < nameAmount && b != phoneNumberAmount) {
    ^
    PhoneBook.java:18: error: variable name might not have been initialized
    if(name != quit) {
    ^
    3 errors

    My apologies for the improper posting
    Last edited by jmcquaid1987; 04-14-2014 at 01:42 AM.

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

    Default Re: PhoneBook Application

    What about the text of the compiler error messages?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default Re: PhoneBook Application

    I get the feeling that there are more issues with my code that are next to come for the compiler.. :/

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,718
    Rep Power
    5

    Default Re: PhoneBook Application

    Take a look at the variables the compiler is complaining about:

    Java Code:
    int a;
    What value should a have? 0? -1? 1000? Java can't know and that last thing Java should do is make assumptions; that's what it is complaining about, you need to set an initial value ('initialize it') yourself so there is no chance that the wrong thing is happening.

    Java Code:
    int a = 0;
    That's all.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Re: Java Phonebook With Arrays // HELP! //
    By Zoul787 in forum New To Java
    Replies: 6
    Last Post: 03-26-2013, 04:21 AM
  2. Re: Java Phonebook With Arrays // HELP! //
    By Zoul787 in forum New To Java
    Replies: 0
    Last Post: 03-19-2013, 11:23 PM
  3. Need help with Phonebook program
    By evvdogg in forum New To Java
    Replies: 11
    Last Post: 12-07-2012, 04:05 AM
  4. Phonebook HashMap Example
    By forms in forum New To Java
    Replies: 3
    Last Post: 01-23-2012, 07:09 PM
  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
  •