Question
Hello, I have created a program that is intended to follow this prompt: Using arrays create a personal phone directory that contains room for first
Hello, I have created a program that is intended to follow this prompt:
Using arrays create a personal phone directory that contains room for first names and phone numbers for 20 people. Using the "Directory.txt" write a program that will read this text file, retrieve the first name and phone number for each person, and store in arrays (parallel).
Once these names are in the arrays, prompt user for a name, search for the name, and if name is found in the array, display the corresponding phone number. If name is not found, prompt the user to enter a phone number, and add the new name and phone number to the arrays. Continue to prompt the user for names until the user enters "quit" or until arrays are full. After the arrays are full (containing 20 names) or the user enters quit, do not allow the user to add new entries. Print the full directory once program ends. Save the file as PhoneNumbers.java.
I have completed the program, but I need assistance fixing my 2D array. Currently the program prints null for the values that aren't filled by the user. I know this is because I have defined the 2D array as [20][2], but I don't know how to make a loop for my array. Please help. Attached is my code, line 15 is where I define the array:
import java.util.*; import java.io.*;
public class PhoneNumbers { public static void main(String[] args) throws FileNotFoundException { File filename = new File("Directory.txt"); Scanner fileRead = new Scanner(filename); Scanner input = new Scanner(System.in);
String[][] directory = new String[20][2]; int linecount = 0; String choice = ""; while(fileRead.hasNextLine()) //while the file is open { String line = fileRead.nextLine(); Scanner linescan = new Scanner(line); String name = linescan.next(); String num = linescan.next(); directory[linecount][0] = name; directory[linecount][1] = num; linecount++; } System.out.println("---------------------------------------------------------------------------------"); System.out.println("Welcome to our Automated Directory!"); while(linecount < 20 && !choice.equalsIgnoreCase("quit")) { System.out.println("---------------------------------------------------------------------------------"); System.out.println("Please make a choice:"); System.out.println("Please enter begin to search our directory."); System.out.println("If you would like to stop the program and print the directory, please enter quit."); System.out.println("---------------------------------------------------------------------------------"); choice = input.next(); if(choice.equalsIgnoreCase("begin")) { System.out.println("Please enter the name you would like to search."); String nameSearch = input.next(); for(int i = 0; i < 20; i++) { if(nameSearch.equalsIgnoreCase(directory[i][0])) { System.out.println("---------------------------------------------------------------------------------"); System.out.println("Here is the number you are looking for: " + directory[i][1]); break; } if(!nameSearch.equalsIgnoreCase(directory[i][0])) { System.out.println("We're sorry, the name you have entered is not in our directory."); System.out.println("Let's add " + nameSearch + " to the directory."); System.out.println("Please enter " + nameSearch + "'s phone number:"); String newNum = input.next(); System.out.println("---------------------------------------------------------------------------------"); directory[linecount][0] = nameSearch; directory[linecount][1] = newNum; linecount++; break; } } } else if(choice.equalsIgnoreCase("quit")) { System.out.println("---------------------------------------------------------------------------------"); System.out.println("Thank you for using our program."); System.out.println("---------------------------------------------------------------------------------"); System.out.println("Printing directory:"); for(int c = 0; c < directory.length; c ++) { for(int j = 0; j < directory[c].length; j++) { System.out.print(directory[c][j]); } System.out.println(); } } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started