Need help with my code (either do-while loop, nested for loop, or my if-then-els
ID: 3567310 • Letter: N
Question
Need help with my code (either do-while loop, nested for loop, or my if-then-else code). After I run the code it will show both the if/else statements that are in the nested for loop in the do-while loop . Here is my code: import java.util.Scanner; public class PhoneBook { public static void main(String[] args) { Scanner input = new Scanner(System.in); String[] name = new String[20]; String[] cellPhone = new String[20]; String[] homePhone = new String[20]; String[] address = new String[20]; String[] email = new String[20]; String nEntry = ""; String hEntry = ""; String cEntry = ""; String aEntry = "" ; String eEntry = ""; String list = ""; int count = 0, x; int answer = 0; boolean isFound = false; System.out.print("ENTER A CONTACT NAME FOR PHONEBOOK: "); nEntry = input.nextLine(); name[count] = nEntry; while(count < name.length && (!nEntry.equals ("Q") && !nEntry.equals ("q"))) { System.out.print("ENTER A HOME PHONE NUMBER: "); hEntry = input.nextLine(); homePhone[count] = hEntry; System.out.print("ENTER A CELL PHONE NUMBER: "); cEntry = input.nextLine(); cellPhone[count] = cEntry; System.out.print("ENTER AN ADDRESS: "); aEntry = input.nextLine(); address[count] = aEntry; System.out.print("ENTER AN E-MAIL ADDRESS: "); eEntry = input.nextLine(); email[count] = eEntry; System.out.println(); ++count; System.out.print("ENTER ANOTHER CONTACT NAME TO CONTINUE OR ENTER Q TO EXIT ENTRY PHASE: "); nEntry = input.nextLine(); name[count] = nEntry; } System.out.println(); System.out.println("NUMBER OF CONTACTS ENTERED: " + count); System.out.println(); for(x = 0; x < count; ++x) { list += "* " + name[x] + " "; } do { System.out.println(list); System.out.print("ENTER A NAME FOR CONTACT INFORMATION: "); nEntry = input.nextLine(); System.out.println(); for(x = 0; x < count; ++x) if (nEntry.equalsIgnoreCase(name[x])) { isFound = true; System.out.println("============================================="); System.out.println("INFORMATION FOR: " + name[x]); System.out.println("---------------------------------------------"); System.out.println("HOME PHONE : " + homePhone[x]); System.out.println("CELL PHONE : " + cellPhone[x]); System.out.println("ADDRESS : " + address[x]); System.out.println("E-MAIL : " + email[x]); System.out.println("============================================="); } else System.out.println("THERE IS NO CONTACT INFORMATION FOR: " + nEntry); System.out.println(); System.out.print("TYPE 1 TO SEARCH MORE CONTACTS OR ANY OTHER NUMBER TO QUIT: "); answer = input.nextInt(); input.nextLine(); }while(answer == 1); System.out.println(); System.out.println("THANK YOU FOR USING MY CONTACT ENTRY AND SEARCH PROGRAM"); } }
Explanation / Answer
import java.util.Scanner;
public class PhoneBook
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] name = new String[20];
String[] cellPhone = new String[20];
String[] homePhone = new String[20];
String[] address = new String[20];
String[] email = new String[20];
String nEntry = "";
String hEntry = "";
String cEntry = "";
String aEntry = "" ;
String eEntry = "";
String list = "";
int count = 0, x; int answer = 0; boolean isFound = false;
System.out.print("ENTER A CONTACT NAME FOR PHONEBOOK: ");
nEntry = input.nextLine();
name[count] = nEntry;
while( count< name.length && (!nEntry.equals ("Q") && !nEntry.equals ("q")))
{
System.out.print("ENTER A HOME PHONE NUMBER: ");
hEntry = input.nextLine();
homePhone[count] = hEntry;
System.out.print("ENTER A CELL PHONE NUMBER: ");
cEntry = input.nextLine();
cellPhone[count] = cEntry;
System.out.print("ENTER AN ADDRESS: ");
aEntry = input.nextLine();
address[count] = aEntry;
System.out.print("ENTER AN E-MAIL ADDRESS: ");
eEntry = input.nextLine();
email[count] = eEntry;
System.out.println();
++count;
System.out.print("ENTER ANOTHER CONTACT NAME TO CONTINUE OR ENTER Q TO EXIT ENTRY PHASE: ");
nEntry = input.nextLine();
name[count] = nEntry;
}
System.out.println();
System.out.println("NUMBER OF CONTACTS ENTERED: " + count);
System.out.println();
for(x = 0; x < count; ++x)
{
list += "* " + name[x] + " ";
}
do
{
System.out.println(list);
System.out.print("ENTER A NAME FOR CONTACT INFORMATION: ");
nEntry = input.nextLine();
System.out.println();
for(x = 0; x < count; ++x)
{
if (nEntry.equalsIgnoreCase(name[x]))
{
isFound = true;
System.out.println("=============================================");
System.out.println("INFORMATION FOR: " + name[x]);
System.out.println("---------------------------------------------");
System.out.println("HOME PHONE : " + homePhone[x]);
System.out.println("CELL PHONE : " + cellPhone[x]);
System.out.println("ADDRESS : " + address[x]);
System.out.println("E-MAIL : " + email[x]);
System.out.println("=============================================");
}
}
if(!isFound)
{
System.out.println("THERE IS NO CONTACT INFORMATION FOR: " + nEntry);
System.out.println();
}
else
isFound= false;
System.out.print("TYPE 1 TO SEARCH MORE CONTACTS OR ANY OTHER NUMBER TO QUIT: ");
answer = input.nextInt(); input.nextLine();
}while(answer == 1);
System.out.println();
System.out.println("THANK YOU FOR USING MY CONTACT ENTRY AND SEARCH PROGRAM");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.