无法解决扫描问题" Java中的错误

时间:2015-03-21 23:23:52

标签: java

我和朋友一起工作,我们正在尝试使用数组来利用文本文件中的数据。我们不断得到扫描无法解决的问题"错误。

这是代码。任何正确方向的点都会非常感激。谢谢!

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

public class EmployeeOrderingDemo 
{    
    public static void main(String[] args) 
    {           
        ArrayList<EmployeeFX> employeeList = new ArrayList<EmployeeFX>();

        try 
        {
            File file = new File("C:\\Users\\Jason\\workspace\\EmpData");
            Scanner scan = new Scanner(file);
        }
        catch(Exception e) 
        {
            System.out.println(e.getMessage());
            System.exit(1);
        }

        scan.nextLine(); // to skip first line in file

        while(scan.hasNextLine()) 
        { // while file has another line
            int id = scan.nextInt(); // read the next integer
            String fName = scan.next(); // read the next string
            String lName = scan.next(); // read the next string
            Boolean sd = scan.nextBoolean(); // read the next boolean
            Long s = scan.nextLong(); // read the next long

            // add variables to the object
            employeeList.add(new EmployeeFX(id, fName, lName, sd, s));
        }

        outputData("Output in ORIGINAL order.", employeeList,
                   EmployeeOrdering.ORIGINAL);
        outputData("Output in SALARIED order.", employeeList,
                   EmployeeOrdering.SALARIED);
        outputData("Output in NAME order.", employeeList,
                   EmployeeOrdering.NAME);
        outputData("Output in EMPLOYEE_ID order.", employeeList,
                   EmployeeOrdering.EMPLOYEE_ID);
        outputData("Output in SALARY order.", employeeList,
                   EmployeeOrdering.SALARY);
    }

    public static void outputData(String str, ArrayList<EmployeeFX> employeeList,
                                  Comparator<EmployeeFX> specificComparator) 
    {
        String headerString = "Id    FirstName    LastName    Salaried    Salary";

        System.out.println("\n" + str + "\n\n" + headerString + "\n");
        Collections.sort(employeeList, specificComparator);
        // will put in output here          
    }    
}

2 个答案:

答案 0 :(得分:1)

当您在scan块范围内定义try时会发生什么。在Java中,范围由块定义(由{}分隔)。

那么,你能做什么?

您可以在外部定义它,并在try块中初始化它:

Scanner scan;
try {
    File file = new File("C:\\Users\\Jason\\workspace\\EmpData");
   scan = new Scanner(file);
}catch(Exception e) {
    System.out.println(e.getMessage());
    System.exit(1);
}

答案 1 :(得分:1)

您的Scanner scan超出了try区域之外的范围。一种解决方案就像是

Scanner scan = null;
try {
    File file = new File("C:\\Users\\Jason\\workspace\\EmpData");
    // Scanner scan = new Scanner(file);
    scan = new Scanner(file);
}catch(Exception e) {
    System.out.println(e.getMessage());
    System.exit(1);
}

或者,您可以将所有逻辑放在try-catch

List<EmployeeFX> employeeList = new ArrayList<>();
try {
    File file = new File("C:\\Users\\Jason\\workspace\\EmpData");
    Scanner scan = new Scanner(file);
    scan.nextLine(); // to skip first line in file
    while (scan.hasNextLine()) { // while file has another line
        int id = scan.nextInt(); // read the next integer
        String fName = scan.next(); // read the next string
        String lName = scan.next(); // read the next string
        Boolean sd = scan.nextBoolean(); // read the next boolean
        Long s = scan.nextLong(); // read the next long

        // add variables to the object
        employeeList.add(new EmployeeFX(id, fName, lName, sd, s));
    }

    outputData("Output in ORIGINAL order.", employeeList,
            EmployeeOrdering.ORIGINAL);
    outputData("Output in SALARIED order.", employeeList,
            EmployeeOrdering.SALARIED);
    outputData("Output in NAME order.", employeeList, EmployeeOrdering.NAME);
    outputData("Output in EMPLOYEE_ID order.", employeeList,
            EmployeeOrdering.EMPLOYEE_ID);
    outputData("Output in SALARY order.", employeeList,
            EmployeeOrdering.SALARY);
} catch (Exception e) {
    System.out.println(e.getMessage());
}