错误地只打印一个数组中的一个对象
Mistakenly printing only one object within an array
作业说明坚持要求我使用数组——而不是数组列表或任何其他可能的选项。此外,任何指定的参数都在那里,因为那是我的教授在她的指示中要求的
基本上,我成功地打印了我需要的对象数量并且条目的格式等都是正确的——但我只是重复地检索一个对象的内容。我试过搞乱静态和非静态的重新分配,但这似乎只会产生更多问题。 TestEmployee4 依赖于一个文本文件,但问题肯定不在我对文本的检索中,因此它本质上是无关紧要的。 TestEmployee4 还依赖于先前使用的 class、ScottEmployee2(这就是为什么它充满评论)。
我唯一的目标是让这个程序正确地 运行 -- 在这一点上,我并不担心这个项目中出现的大量有问题的代码。我要到星期一才能进一步咨询我的教授。
这是 TestEmployee4 的内容:
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();
}
}
}
这是 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: ,000");
}
else
{
// do nothing
}
}
}
所以有几件事我想和你一起讨论,其中一些超出了你所遇到的这个特定问题的答案。
首先,让我们解决手头的问题。
每当你去打印你的员工数组时,你都会执行以下代码:
public static void printEmployeeArray(ScottEmployee2[]employees)
{
for(ScottEmployee2 i : employees){
ScottEmployee2.displayEmployee();
}
}
此代码存在一些问题。首先,您在 ScottEmployee2[] 和员工之间没有 space。接下来,您将调用 class ScottEMployee2 上的 displayEmployee() 而不是 for 循环中的对象。这是你应该做的:
public static void printEmployeeArray(ScottEmployee2[]employees)
{
for(ScottEmployee2 i : employees){
i.displayEmployee();
}
现在,除了这个快速修复之外,我还想和您谈谈您的一些代码约定。首先,这个 while 循环应该像这样做一个 for 循环:
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++;
}
这仅仅是因为它在文体上更合适。其次,在您的 ScottEmployee class 中,您有几个不应该静态的方法。鞋子是:
- displayEmployee();
- checkBonus();
- 设置名称(); //你不希望所有的员工都有相同的名字吧?
- getName();
此外,class 中的几乎所有字段都不应该是静态的,因为它们不需要在 class 的每个实例化过程中保持一致。这意味着您的字段应如下所示:
private static int number;
private int rank;
private String name,department,position;
private double salary,percentage,modSalary;
仅当您应该在 class 的所有实例中进行现场工作时才应使用静态。具有静态数字意味着无论您在制作新的 ScottEmployee2 时将其设置为什么,除非您对其进行更改,否则将是您制作的下一个。
我诚挚地希望所有这些对您的编码冒险有所帮助!请让我知道是否还有什么可以帮助您的!
当您进行 for/each 循环时,您有
ScottEmployee2.displayEmployee();
但是,您为 ScottEmployee2 对象分配了变量 i
。改为尝试:
i.displayEmployee();
还有一些关于您的代码的评论(希望有帮助)。
在你的一种方法中,你有类似的东西:
if(//something) {
//Do something
}
else {
//do nothing
}
然而,您不需要对每个 if 语句都使用 else。你可以简单地做:
if(//something) {
//do something
}
然后离开else部分。这也只是为了约定,但习惯上有像上面代码那样的括号,在 if 语句的同一行上有左括号。总的来说,一个非常简单的错误。
作业说明坚持要求我使用数组——而不是数组列表或任何其他可能的选项。此外,任何指定的参数都在那里,因为那是我的教授在她的指示中要求的
基本上,我成功地打印了我需要的对象数量并且条目的格式等都是正确的——但我只是重复地检索一个对象的内容。我试过搞乱静态和非静态的重新分配,但这似乎只会产生更多问题。 TestEmployee4 依赖于一个文本文件,但问题肯定不在我对文本的检索中,因此它本质上是无关紧要的。 TestEmployee4 还依赖于先前使用的 class、ScottEmployee2(这就是为什么它充满评论)。
我唯一的目标是让这个程序正确地 运行 -- 在这一点上,我并不担心这个项目中出现的大量有问题的代码。我要到星期一才能进一步咨询我的教授。
这是 TestEmployee4 的内容:
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();
}
}
}
这是 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: ,000");
}
else
{
// do nothing
}
}
}
所以有几件事我想和你一起讨论,其中一些超出了你所遇到的这个特定问题的答案。
首先,让我们解决手头的问题。 每当你去打印你的员工数组时,你都会执行以下代码:
public static void printEmployeeArray(ScottEmployee2[]employees)
{
for(ScottEmployee2 i : employees){
ScottEmployee2.displayEmployee();
}
}
此代码存在一些问题。首先,您在 ScottEmployee2[] 和员工之间没有 space。接下来,您将调用 class ScottEMployee2 上的 displayEmployee() 而不是 for 循环中的对象。这是你应该做的:
public static void printEmployeeArray(ScottEmployee2[]employees)
{
for(ScottEmployee2 i : employees){
i.displayEmployee();
}
现在,除了这个快速修复之外,我还想和您谈谈您的一些代码约定。首先,这个 while 循环应该像这样做一个 for 循环:
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++;
}
这仅仅是因为它在文体上更合适。其次,在您的 ScottEmployee class 中,您有几个不应该静态的方法。鞋子是:
- displayEmployee();
- checkBonus();
- 设置名称(); //你不希望所有的员工都有相同的名字吧?
- getName();
此外,class 中的几乎所有字段都不应该是静态的,因为它们不需要在 class 的每个实例化过程中保持一致。这意味着您的字段应如下所示:
private static int number;
private int rank;
private String name,department,position;
private double salary,percentage,modSalary;
仅当您应该在 class 的所有实例中进行现场工作时才应使用静态。具有静态数字意味着无论您在制作新的 ScottEmployee2 时将其设置为什么,除非您对其进行更改,否则将是您制作的下一个。
我诚挚地希望所有这些对您的编码冒险有所帮助!请让我知道是否还有什么可以帮助您的!
当您进行 for/each 循环时,您有
ScottEmployee2.displayEmployee();
但是,您为 ScottEmployee2 对象分配了变量 i
。改为尝试:
i.displayEmployee();
还有一些关于您的代码的评论(希望有帮助)。
在你的一种方法中,你有类似的东西:
if(//something) {
//Do something
}
else {
//do nothing
}
然而,您不需要对每个 if 语句都使用 else。你可以简单地做:
if(//something) {
//do something
}
然后离开else部分。这也只是为了约定,但习惯上有像上面代码那样的括号,在 if 语句的同一行上有左括号。总的来说,一个非常简单的错误。