java - Java 中出现 "scan cannot be resolved"错误

标签 java

我正在与一位 friend 合作,我们正在尝试使用数组来利用文本文件中的数据。我们不断收到“扫描无法解析”错误。

这是代码。任何正确方向的观点都将不胜感激。谢谢!

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          
    }    
}

最佳答案

您的扫描仪扫描超出了try block 之外的范围。一种解决方案类似于

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());
}

关于java - Java 中出现 "scan cannot be resolved"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189206/

相关文章:

java - Java 中的 pretty-print HashSet

java - 同一类中的类似方法

java - Tomcat 7.0 在重新加载应用程序时是否会关闭 DataSource?

java - 这个 jar 构建中缺少什么?

java - UrlConnection.getHeaderField(字符串名称) 返回 null

java - 将头节点移动到链表末尾

javax.tools编译器生成jar

Java 数组上的多线程(分割)

java - 添加新库时出现错误程序类型已存在 : Program type already present: androidx. recyclerview.widget.AdapterHelper$Callback

java - 在 Spring Boot + Spring Integration 应用程序中加密邮件密码