顺序搜索数组

时间:2015-07-15 19:01:36

标签: java arrays sorting sequential

本周我迫切需要更多的帮助。我的教授是低于标准杆,并且没有尽力清理。

问题:  导入文件并搜索用户请求的特定数据。                    输出必须返回类似于:                   顺序发现ID号为77470,价格为49.55美元。                                            要么                    Sequential没有找到ID号77777。

我不知道从哪里开始,或者即使这是正确的......

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

薪资等级:

    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

1 个答案:

答案 0 :(得分:0)

您是从txt文件读入还是在运行程序时输入数据?