Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is my assignment: Create a file called myFriends.dat that contains a list o

ID: 3569121 • Letter: T

Question

This is my assignment:

Create a file called myFriends.dat that contains a list of names of all of your friends (strings). Assume you have a maximum of 250 friends.

Write a java program that does the following:

Read the file into an array.
Pass the array in a function that will sort the array using selection sort.
Request from the keyboard a name to search.
Write a function that will perform a binary search on the array.
Display that the name is a friend or is not a friend depending on if you found it. Continue processing names until END is typed in.

This is what I have, but I keep getting the following error:

Exception in thread "main" java.lang.NullPointerException
   at MyFriends.selectionSort(MyFriends.java:78)
   at MyFriends.main(MyFriends.java:43)

Here is my code:

import java.io.*;          // needed for file and IOException
import java.util.Scanner; // needed for scanner class

public class MyFriends

{

   public static void main(String[] args)throws IOException


      {
      
        final int SIZE = 250;                   //sets size of array as a final variable
        String[] friends = new String[SIZE];    //declare a new String array
        int index = 0;                          //loop control variable
        String searchName;                      //variable to hold name input for search
      
        //open the file for reading
        File file = new File("myFriends.dat");
        Scanner inputFile = new Scanner(file);
      
        //read the file contents into the array
        while (inputFile.hasNext() && index < friends.length)
      
         {
            friends[index] = inputFile.nextLine();
            index++;
          
         }
       
         //close the file
         inputFile.close();
       
         selectionSort(friends);
       
      
        //new scanner for search input
        Scanner stdin = new Scanner(System.in);
    
        //Get name to search
       do
         {
            System.out.print("Please enter a name or END to terminate: ");
            searchName = stdin.nextLine();
            binarySearch(friends,searchName);
          
          
          }while(searchName != "END");  
     
   }
/*

Method to complete the selection sort on the array

*/

public static String[] selectionSort(String[] array)
      
       {
            for (int i = 1; i < array.length; i++)
         
           {
            
                int s = i-1;
          
               for (int j = i; j < array.length; j++)
             
                  {
                      if (array[j].compareTo(array[s]) < 0)
                   
                      
                        s = j;
                    }
              
               String temp = array[i-1];
                array[i-1] = array[s];
                array[s] = temp;
  
            }
            return array;
        }
     
/*

Method to complete the binary search

*/

   public static void binarySearch(String[] array, String value)

      {
    
         int first = 0;
         int last = array.length - 1;
         int middle;
         int position = -1;
         boolean found = false;
       
         while (!found && first <= last)
       
            {
              middle = first + last / 2;
            
              if (array[middle] == value)
            
               {
             
                  found = true;
                  position = middle;
                
               }
             
              else if (array[middle].compareTo(value) > 0)
            
               last = middle -1;
           
              else
            
               first = middle + 1;
             
             }
           
             if (found == true)
             
               System.out.println(value + " is my friend.");
             
             else
           
               System.out.println(value + " is not my friend.");
             
           }
             
       
       

}
  

Explanation / Answer

import java.io.*; // needed for file and IOException
import java.util.Scanner; // needed for scanner class

public class MyFriends

{

public static void main(String[] args)throws IOException


{
  
final int SIZE = 250; //sets size of array as a final variable
String[] friends = new String[SIZE]; //declare a new String array
int index = 0; //loop control variable
String searchName; //variable to hold name input for search
  
//open the file for reading
File file = new File("c:/myFriends.dat");
Scanner inputFile = new Scanner(file);
  
//read the file contents into the array
while (inputFile.hasNext() && index < friends.length)
  
{
friends[index] = inputFile.nextLine();
index++;
  
}

//close the file
inputFile.close();



selectionSort(friends, index);

//new scanner for search input
Scanner stdin = new Scanner(System.in);
  
//Get name to search
do
{
System.out.print("Please enter a name or END to terminate: ");
searchName = stdin.nextLine();
  
  
if(searchName.equalsIgnoreCase("END"))
   break;
  
binarySearch(friends,index, searchName);
  
  
}while(true);

}
/*

Method to complete the selection sort on the array

*/

public static void selectionSort(String[] friends, int size)


  
{
   for (int i = 0; i < size - 1; i++)
{
int index = i;
for (int j = i + 1; j < size; j++)
if (friends[j].compareTo(friends[index]) < 0)
index = j;
  
String smallerNumber = friends[index];
friends[index] = friends[i];
friends[i] = smallerNumber;
}
  
}

/*

Method to complete the binary search

*/


public static void binarySearch(String[] friends, int size, String value)

{
     
   int start = 0;
int end = size -1;
boolean isFound = false;
  
   for (int i = 0; i < size; i++) {
int middle = (end - start)/2;
if (friends[i].equalsIgnoreCase(value)) {
   isFound =true;
}
else if (friends[middle].compareTo(value) > 0) {
end = middle - 1;
}
else {
start = middle + 1;
}
}
     
   if(isFound)
       System.out.println(value + " is my friend.");
   else
       System.out.println(value + " is not my friend.");   




}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote