patternjavaMinor
Can I simplify my phone look-up program?
Viewed 0 times
canlookprogramsimplifyphone
Problem
I needed to make a phone number look-up program with 10 contacts. I feel like I can shorten the program up a bit, it seems very long, but I don't know how. Any suggestion? Do I need all of the Java imports?
```
package phoneLookUp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class phoneLookUp
{
static HashMap contact = new HashMap();
// Method getName() will return the name for given number if it is exist in the contact
public static String getName(String number)
{
Set set = contact.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry me = (Map.Entry)it.next();
String tNum = (String)me.getValue();
if(tNum.equals(number))
{
return (String)me.getKey();
}
}
//if given number is not found
return "";
}
// Method getNumber() will return the Name of the given phone number if it is exist in the contact
public static String getNumber(String name)
{
Set set = contact.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry me = (Map.Entry)it.next();
String tName = (String)me.getKey();
if(tName.equals(name))
{
String formattedNumber = outputFormat((String)me.getValue());
return formattedNumber;
}
}
// if name if not found in the contact
return "";
}
// Formatting the given phone number
public static String outputFormat(String phone)
{
String temp = "(";
for(int i = 0; i < phone.length(); i++)
{
if(i < 2)
temp += phone.charAt(i);
else if(i == 2)
temp += phone.charAt(i)+") ";
else if(i < 5)
temp += phone.charAt(i);
else if(i == 5)
temp += phone.charAt(i)+" - ";
else
temp += phone.charAt(i);
}
return temp;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// contacts
contact.put("Tom","6037571122");
contact.put("Alice","6037779057");
contact.put("George","6037779103");
contact.put("Ben", "6031234322");
contact.put("Jack","6037764522");
contact.put("Greg","6039985434");
contact.put("Franklin","60344
```
package phoneLookUp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class phoneLookUp
{
static HashMap contact = new HashMap();
// Method getName() will return the name for given number if it is exist in the contact
public static String getName(String number)
{
Set set = contact.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry me = (Map.Entry)it.next();
String tNum = (String)me.getValue();
if(tNum.equals(number))
{
return (String)me.getKey();
}
}
//if given number is not found
return "";
}
// Method getNumber() will return the Name of the given phone number if it is exist in the contact
public static String getNumber(String name)
{
Set set = contact.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry me = (Map.Entry)it.next();
String tName = (String)me.getKey();
if(tName.equals(name))
{
String formattedNumber = outputFormat((String)me.getValue());
return formattedNumber;
}
}
// if name if not found in the contact
return "";
}
// Formatting the given phone number
public static String outputFormat(String phone)
{
String temp = "(";
for(int i = 0; i < phone.length(); i++)
{
if(i < 2)
temp += phone.charAt(i);
else if(i == 2)
temp += phone.charAt(i)+") ";
else if(i < 5)
temp += phone.charAt(i);
else if(i == 5)
temp += phone.charAt(i)+" - ";
else
temp += phone.charAt(i);
}
return temp;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// contacts
contact.put("Tom","6037571122");
contact.put("Alice","6037779057");
contact.put("George","6037779103");
contact.put("Ben", "6031234322");
contact.put("Jack","6037764522");
contact.put("Greg","6039985434");
contact.put("Franklin","60344
Solution
Yes , you don't need to iterate through the map to know if it contains a certain value or not, you can just call
You might have notice that I called it
Note: If you need to iterate over a map, don't use iterator, use
any language you use, you have to follow its naming conventions. Use Pascal casing for classes in Java
and not
Use StringBuilder instead of String when you need to append frequently. Every time you do
getString contact = contacts.get(name);
if(contact == null){
// no such value
}You might have notice that I called it
contacts instead of contact, because a map is a collection that holds things and things are plural.Note: If you need to iterate over a map, don't use iterator, use
enhanced-for instead.for(Map.Entry entry: map.entrySet()){
}any language you use, you have to follow its naming conventions. Use Pascal casing for classes in Java
class PhoneLookUpand not
class phoneLookUpUse StringBuilder instead of String when you need to append frequently. Every time you do
+= on a String, it creates a new String because strings are immutable in Java, consider using StringBuilder instead.public static String outputFormat(String phone)
{
StringBuilder temp = new StringBuilder("(");
for(int i = 0; i < phone.length(); i++)
{
if(i < 2){
temp.append(phone.charAt(i));
}
else if(i == 2){
temp.append(phone.charAt(i)+") ");
}
.....
return temp.toString();
}Code Snippets
String contact = contacts.get(name);
if(contact == null){
// no such value
}for(Map.Entry<String,String> entry: map.entrySet()){
}class PhoneLookUpclass phoneLookUppublic static String outputFormat(String phone)
{
StringBuilder temp = new StringBuilder("(");
for(int i = 0; i < phone.length(); i++)
{
if(i < 2){
temp.append(phone.charAt(i));
}
else if(i == 2){
temp.append(phone.charAt(i)+") ");
}
.....
return temp.toString();
}Context
StackExchange Code Review Q#68208, answer score: 3
Revisions (0)
No revisions yet.