-1

I am new to java. I am trying it use sequential search and binary search. I can load the array successfully, but my searches error out. I do not see what i am doing wrong. Are both of my methods having any issues?

package stu.paston.program6;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.InputMismatchException;

public class InventoryData 
{  
private int[] invData = new int[200];
private int count = 0;

InventoryData(){}//Constructor

public void loadArray()
{
    double partNum;
    double price;


try
{
    String filename = "inventoryPricing.dat";
    Scanner infile = new Scanner (new FileInputStream(filename));

    while (infile.hasNextInt())
    {
        invData[count] = infile.nextInt();
        partNum = infile.nextDouble();
        price = infile.nextDouble();

        ++count;
    }
    infile.close();
  }
  catch (IOException ex)
    {
    count = -1;
    ex.printStackTrace();
    }
  }

public int seqSearch(int[] invData, int value)
{
for (int i : invData)
{
if (i==value)
{
    return i;
}
}
return -1;
}

  public int binSearch(int[] invData, int value, int first, int last)
{
 if (first > last)
     return  -1;
 int middle = (first + last) / 2;
 if (invData[middle] == value)
    return middle;
 else if (invData[middle] > value)
    return binSearch(invData, value, first, middle-1);
 else
    return binSearch(invData, value, middle+1, first);
}

public int getCount()
{
return count;
}


public int getOneDataNum(int index)
{
if (index >=0 && index < count)
    return invData[index];
else 
    return -1;
}



}// End InventoryData



package stu.paston.program6;

import java.util.Scanner;

public class MainClass 
{

public static void main(String[] args) 
{
    InventoryData productData = new InventoryData();
    Scanner myScanner = new Scanner(System.in);
    int index;

    productData.loadArray();

    System.out.println("What is the part number you are looking for ->");

    index= myScanner.nextInt();

    if (productData.seqSearch(null, index)>=0)
        System.out.printf("Sequential fount part number #d and the price is                                         %d", index, productData.seqSearch(null, index));
    else 
        System.out.printf("Sequential did not find part number %d", index);
    if (productData.binSearch(null, index, index, index)<=0)
                                                               System.out.printf("\nBinary fount part number #d and the price is %d",   index, productData.binSearch(null, index, index, index));
    else
        System.out.printf("\nBinary did not find part number %d", index);
    myScanner.close();


}

 }
Paston
  • 3
  • 1

1 Answers1

1

You are passing a null reference to seqSearch :

productData.seqSearch(null, index)

Of course you'll get a NullPointerException.

Change

public int seqSearch(int[] invData, int value)

to

public int seqSearch(int value)

You class already has an invData member.

Also change

public int binSearch(int[] invData, int value, int first, int last)

to

public int binSearch(int value, int first, int last)
Eran
  • 387,369
  • 54
  • 702
  • 768