Java 错误地在数组中只打印一个对象
作业说明坚持我使用数组,而不是ArrayList或任何其他可能的选项。此外,任何指定的参数都是专门存在的,因为这是我的教授在她的说明中要求的 基本上,我成功地打印了所需对象的数量和条目的格式等,这些都是正确的——但我只是重复地检索一个对象的内容。我尝试过重新分配静态和非静态的任务,但这似乎只会产生更多的问题。TestEmployee4依赖于一个文本文件,但问题肯定不存在于我对文本的检索中,因此它本质上是无关的。TestEmployee4还依赖于以前使用的类ScottEmployee2(这就是为什么它充满了注释) 我唯一的目标是让这个程序正确运行——在这一点上,我并不担心这个项目中出现大量有问题的代码。我要到星期一才能和教授进一步商量 这是TestEmployee4的内容:Java 错误地在数组中只打印一个对象,java,arrays,project,Java,Arrays,Project,作业说明坚持我使用数组,而不是ArrayList或任何其他可能的选项。此外,任何指定的参数都是专门存在的,因为这是我的教授在她的说明中要求的 基本上,我成功地打印了所需对象的数量和条目的格式等,这些都是正确的——但我只是重复地检索一个对象的内容。我尝试过重新分配静态和非静态的任务,但这似乎只会产生更多的问题。TestEmployee4依赖于一个文本文件,但问题肯定不存在于我对文本的检索中,因此它本质上是无关的。TestEmployee4还依赖于以前使用的类ScottEmployee2(这就是为什
import java.util.*;
import java.io.*;
import java.util.Scanner;
public class TestEmployee4
{
public static void main(String[] args) throws FileNotFoundException
{
ScottEmployee2[] employees = createEmployeeArrayFromFile();
createEmployeeArrayFromFile();
printEmployeeArray(employees);
}
public static ScottEmployee2[] createEmployeeArrayFromFile() throwsFileNotFoundException
{
File file = new File("employees.txt");
Scanner inputFile = new Scanner( new File("employees.txt") );
ScottEmployee2[] employees = new ScottEmployee2[10];
int index = 0;
while (inputFile.hasNextLine() && index < employees.length) // && index < employees.length
{
String dummyNumber = inputFile.nextLine();
int theNumber = Integer.parseInt(dummyNumber);
String theName = inputFile.nextLine();
String theDepartment = inputFile.nextLine();
String thePosition = inputFile.nextLine();
String dummySalary = inputFile.nextLine();
double theSalary = Double.parseDouble(dummySalary);
String dummyRank = inputFile.nextLine();
int theRank = Integer.parseInt(dummyRank);
employees[index] = new ScottEmployee2(theNumber, theName, theDepartment, thePosition, theSalary, theRank);
index++;
}
return employees;
}
public static void printEmployeeArray(ScottEmployee2[]employees)
{
for(ScottEmployee2 i : employees){
ScottEmployee2.displayEmployee();
}
}
}
import java.util.*;
导入java.io.*;
导入java.util.Scanner;
公共类测试人员4
{
公共静态void main(字符串[]args)引发FileNotFoundException
{
ScottEmployee2[]employees=createEmployeeArrayFromFile();
createEmployeeArrayFromFile();
printEmployeeArray(员工);
}
公共静态ScottEmployee2[]createEmployeeArrayFromFile()throwFileNotFoundException
{
File File=新文件(“employees.txt”);
扫描仪输入文件=新扫描仪(新文件(“employees.txt”);
ScottEmployee2[]员工=新ScottEmployee2[10];
int指数=0;
而(inputFile.hasNextLine()&&index
这是ScottEmployee2的内容:
public class ScottEmployee2
{
private static int number;
private static String name;
private static String department;
private static String position;
private static double salary;
private static int rank;
private static double percentage;
private static double modSalary;
public ScottEmployee2(int theNumber, String theName, String theDepartment,String thePosition, double theSalary, int theRank)
{
number = theNumber;
name = theName;
department = theDepartment;
position = thePosition;
salary = theSalary;
rank = theRank;
}
public ScottEmployee2(int theNumber, String theName)
{
number = theNumber;
name = theName;
department = null;
position = null;
salary = 0;
rank = 0;
percentage = 0;
modSalary = 0;
}
/**
* Sets the salary.
* @param theSalary Holds the value of salary.
*/
public void setSalary(double theSalary)
{
salary = theSalary;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project.
* @return salary, a double value
*/
public double getSalary()
{
return salary;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. setNumber is the mutator.
* @param theNumber Stores an integer, the value of a number.
*/
public void setNumber(int theNumber)
{
number = theNumber;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. getNumber is the accessor.
* @return number, an integer.
*/
public int getNumber()
{
return number;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. setName is the mutator.
* @param theName Stores a String, a name.
*/
public static void setName(String theName)
{
name = theName;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. getName is the accessor because
* it gets a value from a class field but does not modify it.
* @return name, a String, the employee's name.
*/
public static String getName()
{
return name;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. theDepartment is the mutator because
* it stores or changes a value in a field.
* @param theDepartment Stores a String, the department that the employee works in.
*/
public void setDepartment(String theDepartment)
{
department = theDepartment ;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. getDepartment is the accessor because
* it gets a value from a class field but does not modify it.
* @return department, a String, the employee's department.
*/
public String getDepartment()
{
return department;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. thePosition is the mutator because
* it stores or changes a value in a field.
* @param thePosition Stores a String, the position that the employee holds.
*/
public void setPosition(String thePosition)
{
position = thePosition ;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. getPosition is the accessor because
* it gets a value from a class field but does not modify it.
* @return position, a String, the position that the employee holds.
*/
public String getPosition()
{
return position;
}
/**
* Created to provide the accessor and mutator methods for each field value
* as instructed to meet the requirements of the project. theDepartment is the mutator because
* it stores or changes a value in a field.
* @param theRank Stores an integer, the employee's rank.
*/
public void setRank(int theRank)
{
rank = theRank;
}
/**
*Accessor method.
*@return rank, an integer, the employee's rank.
*/
public int getRank()
{
return rank;
}
/**
* Mutator method.
* @param percent, stores a double, the percentage
* to be applied to the current salary.
* Contains an if statement, to filter out results
* that are out of bounds -- less than 1% or greater than 25%
*/
public void applyRaise(double percent)
{
percentage = percent;
if (percentage < 1 || percentage > 25)
{
System.out.println("NO RAISE APPLIED");
percentage = 0;
}
modSalary = salary;
salary = modSalary + (salary * (percentage * 0.01));
}
/**
* Accessor method.
* @return percentage, the percent to be applied to salary
* to give the raise.
*/
public double theRaise()
{
return percentage;
}
/**
* Prints a formatted salary. Per instructions, this method does
* not define any parameters or return any values.
*/
public void printSalary()
{
System.out.printf("$%,.2f\n", salary);
}
/**
* Method that returns a boolean value of true if employee rank is greater
* than five. Otherwise, it returns false.
*/
public static boolean checkBonus()
{
{
if (rank > 5)
{
return true;
}
else
{
return false;
}}
}
/**
* Method to print employee's information to standard output. The employee number is formatted to
* nine digits and salary is formatted as currency. This method calls checkBonus() for a value of
* true or false. If true, an additional statement is printed. Otherwise, no bonus statement is printed.
*/
public static void displayEmployee()
{
System.out.println("Name: " + name);
System.out.printf("Employee Number: %09d", number);
System.out.println("\nDepartment: " + department + "\nPosition: " + position);
System.out.printf("Salary: $%,.2f", salary);
System.out.println("\nRank: " + rank);
if (checkBonus() == true)
{
System.out.println("Bonus: $1,000");
}
else
{
// do nothing
}
}
}
公共类ScottEmployee2
{
私有静态整数;
私有静态字符串名;
私人静电管系;
私有静态字符串位置;
私人固定双薪制;
私有静态整数秩;
私人静态双百分比;
私人静态双薪制;
public Scottemplayee2(输入数字,输入名称,输入部门,输入位置,输入两倍长度,输入银行)
{
数字=数字;
名称=名称;
部门=部门;
位置=位置;
薪水=薪水;
秩=秩;
}
public ScottEmployee2(整数,字符串,名称)
{
数字=数字;
名称=名称;
部门=空;
位置=空;
工资=0;
秩=0;
百分比=0;
modSalary=0;
}
/**
*设定工资。
*@param theSalary拥有工资的价值。
*/
公共空集水尺(双倍水尺)
{
薪水=薪水;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*按照指示,以满足项目的要求。
*@返回工资,双倍值
*/
公共双薪制
{
返回工资;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*按照指示,以满足项目的要求。setNumber是变量。
*@param theNumber存储一个整数,即数字的值。
*/
公共无效集合号(整数)
{
数字=数字;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*按照指示,以满足项目的要求。getNumber是访问器。
*@返回数字,一个整数。
*/
public int getNumber()
{
返回号码;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*按照指示,以满足项目的要求。setName是变体。
*@param theName存储一个字符串,一个名称。
*/
公共静态void setName(字符串名称)
{
名称=名称;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*按照指示满足项目的要求。getName是访问器,因为
*它从类字段中获取值,但不修改它。
*@return name,一个字符串,员工姓名。
*/
公共静态字符串getName()
{
返回名称;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*根据指示,以满足项目要求。该部门是变更方,因为
*它在字段中存储或更改值。
*@param theDepartment存储一个字符串,即员工工作的部门。
*/
公共部门(部门字符串)
{
部门=部门;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*按照指示满足项目要求。getDepartment是访问者,因为
*它从类字段中获取值,但不修改它。
*@return department,一串,员工所在部门。
*/
公共部门()
{
退货部;
}
/**
*创建以为每个字段值提供访问器和mutator方法
*按照指示,以满足项目的要求。该位置是变异的,因为
*它在字段中存储或更改值。
*@param thePosition存储一个字符串,即员工h
public static void printEmployeeArray(ScottEmployee2[]employees)
{
for(ScottEmployee2 i : employees){
ScottEmployee2.displayEmployee();
}
}
public static void printEmployeeArray(ScottEmployee2[]employees)
{
for(ScottEmployee2 i : employees){
i.displayEmployee();
}
for (i = 0; i < employees.length i ++){
if(!inputFile.hasNextLine()){
break;
}
String dummyNumber = inputFile.nextLine();
int theNumber = Integer.parseInt(dummyNumber);
String theName = inputFile.nextLine();
String theDepartment = inputFile.nextLine();
String thePosition = inputFile.nextLine();
String dummySalary = inputFile.nextLine();
double theSalary = Double.parseDouble(dummySalary);
String dummyRank = inputFile.nextLine();
int theRank = Integer.parseInt(dummyRank);
employees[index] = new ScottEmployee2(theNumber, theName, theDepartment, thePosition, theSalary, theRank);
index++;
}
private static int number;
private int rank;
private String name,department,position;
private double salary,percentage,modSalary;
ScottEmployee2.displayEmployee();
i.displayEmployee();
if(//something) {
//Do something
}
else {
//do nothing
}
if(//something) {
//do something
}