-2

i am in desperate need of more help this week. My professor is sub par and makes no effort to clear things up.

The Problem: import a file and search for a specific piece of data that is requested by a user. The output must return something similar to: Sequential found ID number 77470, and its price is $49.55. or Sequential did not find ID number 77777.

I have no idea where to go from here, or even if this is correct....

public class MainClass 
{
    public static void main(String[] args) 
    {
        Payroll acmePay = new Payroll();
        Scanner myScanner = new Scanner(System.in);
        int target;



acmePay.loadEmpNums();

        System.out.println("Enter the product number you would like to search: ");
        target = myScanner.nextInt();


        System.out.print(acmePay.seqSearch(target));


        myScanner.close();

    }//END main
}//END class MainClass

Payroll Class:

    public class Payroll 
   {
    private int[] empNums = new int[1000];
    private int empCount = 0;

    Payroll(){} //Currently nothing done in constructor

    public void loadEmpNums()
    {
        String name;
        double salary;
        empCount = 0;       //Just to make sure!

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

            while (infile.hasNext())
            {
                //Read a complete record
                empNums[empCount] = infile.nextInt();
                name = infile.nextLine();
                salary = infile.nextDouble();

                //Increment the count of elements
                ++empCount;
            }

            infile.close();
        }
        catch (IOException ex)
        {
            //If file has problems, set the count to -1
            empCount = -1;
            ex.printStackTrace();
        }
    }//END loadEmpNums

    public int seqSearch (int target)
    {
        int ind = 0;
        int found = -1;

        while (ind < empCount) {
          if(target==empNums[ind])
            {
              found = ind;
              ind = empCount;
            }
          else
            {
              ++ind;
            }
        }
        return found;
    }

}//END class Payroll
maskacovnik
  • 3,080
  • 5
  • 20
  • 26
  • Have you written this particular bit of code yourself? – xrisk Jul 15 '15 at 20:48
  • try to understand the 'seqSearch' method and what it would return in case the 'product number' is found and when the 'product number' is not found. Then you can get the output you want. – anish Jul 15 '15 at 21:20

1 Answers1

0

Are you reading in from a txt file or are they entering the data in when you run the program?

Tanner W
  • 1
  • 1