Java 如何更改扫描仪的输入
我有点困在这里了。目前我正在为学校做一个项目。我最近发现代码应该从文件中读取,而不是从用户输入中读取。我有没有办法改变我的流程?我需要它读取的文件是quartlyemployeedata.txt 这是我的主要课程: MonthlyPayroll.javaJava 如何更改扫描仪的输入,java,eclipse,Java,Eclipse,我有点困在这里了。目前我正在为学校做一个项目。我最近发现代码应该从文件中读取,而不是从用户输入中读取。我有没有办法改变我的流程?我需要它读取的文件是quartlyemployeedata.txt 这是我的主要课程: MonthlyPayroll.java import java.util.*; class MonthlyPayrollDriver { private static final int WORKFORCE_SIZE = 3; private static Scan
import java.util.*;
class MonthlyPayrollDriver
{
private static final int WORKFORCE_SIZE = 3;
private static Scanner inputScan;
public static void main (String[] args)
{
// Set up array of employees
// Note the objects in the array could be in subclasses of Employee
Employee[] employeeList = new Employee[WORKFORCE_SIZE];
inputScan = new Scanner(System.in);
for(int empNum = 0; empNum < WORKFORCE_SIZE; empNum++)
{
System.out.print("Enter employee first name and last name: ");
String first = inputScan.next();
String last = inputScan.next();
System.out.print("\nPlease determind the Status code (F for full-time, P for part time, and I for intern) ");
char response = inputScan.next().charAt(0);
{
if (response == 'f' || response == 'F')
{
System.out.print("\nPlease determind the Status Code (S for salaried or H for hourly) ");
{
char response1 = inputScan.next().charAt(0);
if (response1 == 'h' || response1 == 'H')
{
System.out.print("\nEnter hourly rate: ");
double hourlyRate = inputScan.nextDouble();
System.out.print("\nEnter hours worked last Month: ");
inputScan.nextDouble();
employeeList[empNum] = new HourlyEmployee(last,first,hourlyRate);
}
char response2 = inputScan.next().charAt(0);
if (response2 == 's' || response2 == 'S')
{
System.out.print("Enter annual salary: ");
double annualPay = inputScan.nextDouble();
employeeList[empNum] = new SalariedEmployee(last,first,annualPay);
}
}
}
if (response == 'p' || response == 'P')
{
System.out.print("\nEnter hourly rate: ");
double hourlyRate = inputScan.nextDouble();
System.out.print("\nEnter hours worked last month: ");
inputScan.nextDouble();
employeeList[empNum] = new HourlyEmployee(last, first,
hourlyRate);
}
if (response == 'i' || response == 'I')
{
System.out.print("\nPlease determind the Status Code (H for hourly or U for unpaid.) ");
{
char response1 = inputScan.next().charAt(0);
if (response1 == 'H' || response1 == 'h')
{
System.out.print("\nEnter hourly rate: ");
double hourlyRate = inputScan.nextDouble();
System.out.print("\nEnter hours worked last month: ");
inputScan.nextDouble();
employeeList[empNum] = new HourlyEmployee(last, first,
hourlyRate);
}
else
{
employeeList[empNum] = new HourlyEmployee(last,first,0);
}
}
}
}
}
System.out.println("\n\nMonthly Payroll\n");
for(int j = 0; j < WORKFORCE_SIZE; j++)
{
System.out.println(employeeList[j]);
}
}
}
当然,找零
inputScan = new Scanner(System.in);
到
其中filePath是您需要读取的文件的路径。javadoc说
构造一个新的扫描仪,该扫描仪生成从指定文件扫描的值
这是使您的程序从标准输入(默认为控制台)读取的原因:
inputScan = new Scanner(System.in);
您可以通过两种方式进行更改:
使用操作系统特定语法创建文件,或
创建从文件读取的扫描仪。
第一个选项是这样做的:
java MonthlyPayrollDriver <my_input_file.txt
File file = new File("my_input_file.txt");
inputScan = new Scanner(file);
对于第一个选项,我要更改什么才能使其工作?@开始时,您不需要更改任何内容使第一个选项工作,甚至不需要重新编译。@开始时,您这样运行它时会发生什么?你有错误吗?你有什么收获吗?试着在调试器中运行它,逐步检查代码,看看发生了什么。我正在尝试第二种方法,因为我意识到我会使用它。现在,我在MonthlyPayrollDriver.mainMonthlyPayrollDriver.java:29的线程main java.lang.NullPointerException中遇到异常,因为某些原因,它没有拾取文件。如果它位于不同计算机的不同位置,又会怎样
File file = new File("my_input_file.txt");
inputScan = new Scanner(file);