0

I am writing a program that will take a disk file with an array of strings with a max size of 100 and do a few things to them. However, I am getting a console error due to my bubblesort method searching for and trying to sort 100 (the length of the array) items while the disk file may have, lets say, 15 items. The bubblesort method is returning a console error due to it searching for the remaining 85 items and trying to sort 85 blank items. I am totally lost on how to fix this problem. Is it possible to get the items in the disk file, pass them onto another array and then bubblesort that one so the 85 blank spots are eliminated? Or is adding a counter an option that will solve my problem? I am fairly new at java coding so any help would be greatly appreciated.

Here is the error i am getting:

Exception in thread "main" java.lang.NullPointerException at java.lang.String.compareTo(Unknown Source) at Assignment7.bubbleSort(Assignment7.java:52) at Assignment7.main(Assignment7.java:12)

here is my code:

 import java.io.*;
 import java.util.*;

public class Code3 {

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


    String[] list2, targets, list1;
    list1 = getInput("C:\\Users\\Steave\\Desktop\\file14.txt");
    list2 = bubbleSort(list1);                                            
    targets = getInput("C:\\Users\\Steave\\Desktop\\upload6.txt");

    double seqAvg = seqSearch(list1, targets);
    double binAvg = binSearch(list2, targets);
    System.out.println("Average number of searches for the seqeuntial search is " + seqAvg);
    System.out.println("Average number of searches for the binary search is " + binAvg);

}//end of main method

public static String[] getInput(String filename) throws IOException {
    String[] inputArr = new String[100];

    Scanner in = new Scanner(new File(filename));
    int count = 0;
    while(in.hasNext()) {  
        if (count < 100){
            inputArr[count++] = in.next();
        } else {
            break;
        }

        }       
    in.close();
    return inputArr;

}// end getInput method



 //This method will sort the array and pass it onto a list.
public static String[]bubbleSort(String[] inputArr) {

    String[] Array2 = inputArr.clone();

    for (int i = 0; i<Array2.length; i++)
    {
      for (int j = 0; j<Array2.length-1; j++)
      {
          if (Array2[j].compareTo(Array2[j+1]) > 0)         //CONSOLE ERROR HERE
          {
            String temp = Array2[j];
            Array2[j] = Array2[j+1];
            Array2[j+1] = temp;
          }
       }
    } 

    return Array2;

}// End of sort method.

//This method will do a sequential search on the list1
public static double seqSearch(String[] list1, String[] targets){
      {
          for (int j = 0; j < list1.length; j++)
          {
              for(String str:targets){
              if(list1[j].equalsIgnoreCase(str)){
                  return j;
              }
          }
             {
                return j;
             }
         }
         return -1;
       }

}//end of sequentialSearch method


    //This method will do a binary search on the list   
  public static int binSearch(String[] list1, String[] targets) {
       int lo = 0;
        int hi = list1.length - 1;
        int mid = -1;

        while( lo <= hi ) {
          mid = (lo+hi)/2;

          for(String str:targets){
          if(list1[mid].equalsIgnoreCase(str)){
              return mid;
          }

          hi = mid-1;


          } for(String str:targets){
          if(list1[mid].equalsIgnoreCase(str)){
              return mid;
          }
      }

            lo = mid+1;
           {
            return mid;
          }
        }
        return mid;
      }

  }
Steave
  • 29
  • 5
  • In your `getInput( ... )` method, you know the number of items. It is saved in the variable `count`. Make it global and then run your bubble sort not to `array.length` but till `count`. – denvercoder9 Nov 02 '16 at 17:39
  • In the `getInput` method, if `count == 99` then your code is attempting to assign `inputArr[100] = in.next`, but `inputArr[100]` does not exist. – PEF Nov 02 '16 at 17:40

1 Answers1

1

Some elements of your string array were not initialized. Only the values you read from the input file are assigned a value, the rest are null. An easy fix is to return a new array created from the beginning part that has the assigned values.

Change this line:

return inputArr;

To this:

return Arrays.copyOf(inputArr, count);
janos
  • 120,954
  • 29
  • 226
  • 236