Я работаю с другом, и мы пытаемся использовать массив для использования данных в текстовом файле. Мы продолжаем получать ошибку "проверка не может быть решена".
Вот код. Любая точка в правильном направлении была бы высоко оценена. Благодарю!
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
}
}
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());
}
Случается, что когда вы определяете scan
в области 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);
}