我正在与一位 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/