Skip to content

Instantly share code, notes, and snippets.

@kurtkaiser
Created August 12, 2018 16:28
Show Gist options
  • Save kurtkaiser/ea19c90cd4eef74c5ceee054b0a719ba to your computer and use it in GitHub Desktop.
Save kurtkaiser/ea19c90cd4eef74c5ceee054b0a719ba to your computer and use it in GitHub Desktop.
This was an assignment for an advance college course at my local community college. The assignment gives you much of the code, it combines two seperate arrays, names and phone numbers, into a single multidimensional array. It allows the user the to look up numbers too.
// Kurt Kaiser
// CTIM-168 E40
// 7.13.2018
import java.util.Scanner;
public class PhoneBook
{
/* Creates multidimensional array of numbers and
asking for users input, calls lookup and outputs
the returned result
*/
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
String[][] listings = {
{"Michael Myers", "333-8000"},
{"Ash Williams", "333-2323"},
{"Jack Torrance", "333-6150"},
{"Freddy Krueger", "339-7970"}
};
System.out.println("Enter name to look up.");
String targetName = kbd.nextLine();
String targetPhone = lookupName(targetName, listings);
System.out.println("The phone number is " + targetPhone);
}
/* Loops through the multidimensional array of names to return
the associated number
*/
public static String lookupName(String targetName, String listings[][]){
for (int i = 0; i < 4; i++){
if (targetName.equals(listings[i][0])){
//System.out.println("Number: " + listings[i][1]);
return listings[i][1];
}
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment