I want my code to scan if there are multiple entries with the same number. if so
ID: 3718464 • Letter: I
Question
I want my code to scan if there are multiple entries with the same number. if so, when the second number and on with the same number are inputted, the program exits without saving the entry to the array and sends the the user back to the start.
If a hashset is needed, what should it look like?
Here's my the relevant parts of my code so far :
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
class Entry {
public String fname, lname, number, note;
}
class PBN {
public static Entry[] contactList;
public static int num_entries;
public static Scanner stdin = new Scanner(System.in);
public static void main(String args[]) throws Exception{
int i;
char C;
String code, Command;
contactList = new Entry[999];
num_entries = 0;
try {
readPhoneBook("PhoneBook.txt");
} catch (FileNotFoundException e) {}
System.out.println("Codes are entered as 1 to 8 characters. " +
"Use Commands: " +
" "e" for enter a new contact, " +
" "f" for find contact by fist name, " +
" "r" for find contact by last name, " +
" "y" for find contact by phone number, " +
" "l" for listing all the existing contacts, " +
//" "m" for merging entries.
" "d" for removing contacts by phone number, " +
" "a" for sort alphabetically by first name, " +
" "n" for sort alphabetically by last name, " +
" "p" for sort by number, " +
" "q" to quit.");
Command = null;
C = ' ';
while(true) { // loop infinitely
System.out.print("Command: ");
Command = stdin.nextLine();
C = Command.charAt(0);
switch (C) {
case 'e': addContact(); break;
default:
System.out.println("Invalid command Please enter the command again!!!");
}
}
}
public static void readPhoneBook(String FileName) throws Exception {
File F;
F = new File(FileName);
Scanner S = new Scanner(F);
while (S.hasNextLine()) {
contactList[num_entries]= new Entry();
contactList[num_entries].fname = S.next();
contactList[num_entries].lname = S.next();
contactList[num_entries].number = S.next();
contactList[num_entries].note = S.nextLine();
num_entries++;
}
S.close();
}
public static void addContact() {
System.out.print("Enter first name: ");
String fname = stdin.nextLine();
String lname;
String number;
String pattern = "^\(?(\d{3})?\)?[- ]?(\d{3})[- ](\d{4})$";
while (fname.length() > 8 || fname.length() < 1) {
System.out.println("First name must be between 1 to 8 characters.");
System.out.print("Enter first name: ");
fname = stdin.nextLine();
}
contactList[num_entries] = new Entry();
contactList[num_entries].fname = fname;
System.out.print("Enter last name: ");
lname = stdin.nextLine();
while (lname.length() > 8 || lname.length() < 1) {
System.out.println("First name must be between 1 to 8 characters.");
System.out.print("Enter first name: ");
lname = stdin.nextLine();
}
contactList[num_entries].lname = lname;
System.out.print("Enter Number: ");
number = stdin.nextLine(); // read the number
while(!number.matches(pattern)) { // as long as user doesnt enters correct format, loop
System.out.println("Error!");
System.out.println("Not proper digit format! Use "012-3456", "(012)345-6789"" +
", or "012-345-6789" format.");
System.out.print("Enter number: ");
number = stdin.nextLine();
}
for (Entry e : contactList) {
if (e.number.equals(number)) {
System.out.println("This phone number already exist. Please check contacts.");
System.out.println("");
return;
}else{
break;
}
}
contactList[num_entries].number = number;
System.out.print("Enter Notes: ");
contactList[num_entries].note = stdin.nextLine();
num_entries++;
System.out.println();
}
Explanation / Answer
The code after adding HashSet is as follows:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.HashSet;
class Entry {
public String fname, lname, number, note;
}
class PBN {
public static Entry[] contactList;
public static HashSet<String> h = new HashSet(); //defining the hashset
public static int num_entries;
public static Scanner stdin = new Scanner(System.in);
public static void main(String args[]) throws Exception{
int i;
char C;
String code, Command;
contactList = new Entry[999];
num_entries = 0;
try {
readPhoneBook("PhoneBook.txt");
} catch (FileNotFoundException e) {}
System.out.println("Codes are entered as 1 to 8 characters. " +
"Use Commands: " +
" "e" for enter a new contact, " +
" "f" for find contact by fist name, " +
" "r" for find contact by last name, " +
" "y" for find contact by phone number, " +
" "l" for listing all the existing contacts, " +
//" "m" for merging entries.
" "d" for removing contacts by phone number, " +
" "a" for sort alphabetically by first name, " +
" "n" for sort alphabetically by last name, " +
" "p" for sort by number, " +
" "q" to quit.");
Command = null;
C = ' ';
while(true) { // loop infinitely
System.out.print("Command: ");
Command = stdin.nextLine();
C = Command.charAt(0);
switch (C) {
case 'e': addContact(); break;
default:
System.out.println("Invalid command Please enter the command again!!!");
}
}
}
public static void readPhoneBook(String FileName) throws Exception {
File F;
F = new File(FileName);
Scanner S = new Scanner(F);
while (S.hasNextLine()) {
contactList[num_entries]= new Entry();
contactList[num_entries].fname = S.next();
contactList[num_entries].lname = S.next();
contactList[num_entries].number = S.next();
contactList[num_entries].note = S.nextLine();
num_entries++;
}
S.close();
}
public static void addContact() {
System.out.print("Enter first name: ");
String fname = stdin.nextLine();
String lname;
String number;
String pattern = "^\(?(\d{3})?\)?[- ]?(\d{3})[- ](\d{4})$";
while (fname.length() > 8 || fname.length() < 1) {
System.out.println("First name must be between 1 to 8 characters.");
System.out.print("Enter first name: ");
fname = stdin.nextLine();
}
contactList[num_entries] = new Entry();
contactList[num_entries].fname = fname;
System.out.print("Enter last name: ");
lname = stdin.nextLine();
while (lname.length() > 8 || lname.length() < 1) {
System.out.println("First name must be between 1 to 8 characters.");
System.out.print("Enter first name: ");
lname = stdin.nextLine();
}
contactList[num_entries].lname = lname;
System.out.print("Enter Number: ");
number = stdin.nextLine(); // read the number
while(!number.matches(pattern)) { // as long as user doesnt enters correct format, loop
System.out.println("Error!");
System.out.println("Not proper digit format! Use "012-3456", "(012)345-6789"" +
", or "012-345-6789" format.");
System.out.print("Enter number: ");
number = stdin.nextLine();
}
if(h.contains(number)){ //checking if number is already present
System.out.println("This phone number already exist. Please check contacts.");
System.out.println("");
return;
}
else{ //number is not present
h.add(number); //add it to hashset
contactList[num_entries].number = number;
System.out.print("Enter Notes: ");
contactList[num_entries].note = stdin.nextLine();
num_entries++;
System.out.println();
}
}
}
//while removing a contact you must also remove the number from hashset by using h.remove(number)
Sample output:
radas-macOS:Desktop radas$ java PBN
Codes are entered as 1 to 8 characters.
Use Commands:
"e" for enter a new contact,
"f" for find contact by fist name,
"r" for find contact by last name,
"y" for find contact by phone number,
"l" for listing all the existing contacts,
"d" for removing contacts by phone number,
"a" for sort alphabetically by first name,
"n" for sort alphabetically by last name,
"p" for sort by number,
"q" to quit.
Command: e
Enter first name: first
Enter last name: last
Enter Number: 012-1234
Enter Notes: abc
Command: e
Enter first name: first
Enter last name: last
Enter Number: 012-1234
[012-1234]
This phone number already exist. Please check contacts.
Command:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.