Results 1 to 6 of 6
Thread: PhoneBook Application
- 04-13-2014, 11:52 PM #1
Member
- Join Date
- Mar 2014
- Posts
- 12
- Rep Power
- 0
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!
- 04-14-2014, 12:09 AM #2
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.
- 04-14-2014, 01:31 AM #3
Member
- Join Date
- Mar 2014
- Posts
- 12
- Rep Power
- 0
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(); } }
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 postingLast edited by jmcquaid1987; 04-14-2014 at 01:42 AM.
- 04-14-2014, 01:38 AM #4
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.
- 04-14-2014, 01:43 AM #5
Member
- Join Date
- Mar 2014
- Posts
- 12
- Rep Power
- 0
Re: PhoneBook Application
I get the feeling that there are more issues with my code that are next to come for the compiler.. :/
- 04-14-2014, 09:51 AM #6
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: PhoneBook Application
Take a look at the variables the compiler is complaining about:
Java Code:int a;
Java Code:int a = 0;
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
Similar Threads
-
Re: Java Phonebook With Arrays // HELP! //
By Zoul787 in forum New To JavaReplies: 6Last Post: 03-26-2013, 04:21 AM -
Re: Java Phonebook With Arrays // HELP! //
By Zoul787 in forum New To JavaReplies: 0Last Post: 03-19-2013, 11:23 PM -
Need help with Phonebook program
By evvdogg in forum New To JavaReplies: 11Last Post: 12-07-2012, 04:05 AM -
Phonebook HashMap Example
By forms in forum New To JavaReplies: 3Last Post: 01-23-2012, 07:09 PM -
phonebook update
By nanna in forum New To JavaReplies: 5Last Post: 03-09-2009, 10:13 PM
Bookmarks