
Patrick B. answered 04/25/21
Math and computer tutor/teacher
import java.io.*;
import java.util.Scanner;
class Friends
{
protected BufferedReader bufferedReader;
protected Scanner scanner;
public Friends()
{
try
{
bufferedReader = new BufferedReader( new FileReader("friends.txt"));
scanner = new Scanner(System.in);
}
catch (IOException ex)
{
System.out.println(" Error opening input file ");
bufferedReader = null;
}
}
//***** RETURNS THE # of FRIENDS IN THE FILE !!!!! *************/
protected int OpenFile()
{
int iNumFriendsReturn=-1;
if (bufferedReader!=null)
{
try
{
String inbuff = bufferedReader.readLine();
iNumFriendsReturn = Integer.parseInt(inbuff.trim());
}
catch (IOException ex)
{
iNumFriendsReturn=-1;
System.out.println(" Error reading input file ");
}
}
return(iNumFriendsReturn);
}
protected int fillArray(String friendNames[], String friendPhones[])
{
int iReturn=0;
int N = friendNames.length;
if (N==friendPhones.length)
{
try
{
for (int iLoop=0; iLoop<N; iLoop++)
{
String inbuff = bufferedReader.readLine();
String tokens[] = inbuff.split(",");
friendNames[iLoop] = new String(tokens[0]);
friendPhones[iLoop] = new String(tokens[1]);
}
}
catch (IOException ex)
{
System.out.println(" Error reading input file ");
iReturn=-1;
}
}
else
{
System.out.println(" memory allocation error ");
iReturn=-2;
}
return(iReturn);
}
public int findAFriend(String friendNames[], String friendPhones[], String targetFriendName)
{
int iReturn=-1;
int N = friendNames.length;
if (N==friendPhones.length)
{
String TargetFriendName = new String(
(targetFriendName.trim()).toUpperCase()
);
for (int iLoop=0; iLoop<N; iLoop++)
{
String curFriendName = new String(
(friendNames[iLoop].trim()).toUpperCase()
);
if (curFriendName.compareTo(TargetFriendName)==0)
{
iReturn = iLoop;
break;
}
}
}
else
{
iReturn = -1;
System.out.println("Error data corrupted ");
}
return(iReturn);
}
public void showFriendsList(String friendNames[], String friendPhones[], String strMsg)
{
int N = friendNames.length;
if (N==friendPhones.length)
{
if (strMsg!=null)
{
System.out.println("*******************************************");
System.out.println(strMsg);
}
System.out.println("**********************************************");
System.out.println("\n # NAME PHONE \n -------------------------------\n");
for (int iLoop=0; iLoop<N; iLoop++)
{
System.out.println( (iLoop+1) + " " + friendNames[iLoop] + " " + friendPhones[iLoop]);
}
}
else
{
System.out.println("Error : Data corrupted ");
}
}
protected void _go(String friendNames[], String friendPhones[])
{
String targetFriendName = "?????";
while (targetFriendName.compareTo("QUIT")!=0)
{
showFriendsList(friendNames,friendPhones,"FRIENDS LIST ");
System.out.println("\n\n");
System.out.print(" Input the friend name or QUIT :> ");
targetFriendName = (scanner.nextLine()).toUpperCase();
if (targetFriendName.compareTo("QUIT")!=0)
{
int iIndexPos = findAFriend(friendNames,friendPhones,targetFriendName);
if (iIndexPos>=0)
{
System.out.println( (iIndexPos+1) + friendNames[iIndexPos] + " " + friendPhones[iIndexPos]);
}
else
{
System.out.println(" Friend " + targetFriendName + " not found ");
}
}
}
}
public int Go()
{
int numFriends = OpenFile();
String friendNames[];
String friendPhones[];
if (numFriends>1)
{
friendNames = new String[numFriends];
friendPhones = new String[numFriends];
if (fillArray(friendNames,friendPhones)==0)
{
_go(friendNames,friendPhones);
}
}
return(numFriends);
}
public static void main(String args[])
{
Friends x = new Friends();
x.Go();
}
}