错误地只打印数组中的一个对象

时间:2018-05-06 01:11:08

标签: java arrays project

作业的说明坚持我使用阵列 - 不是arraylists或任何其他潜在的选择。此外,任何指定的参数都是专门的,因为这是我的教授在她的指示中所要求的

基本上,我成功打印了我需要的对象数量和条目的格式等是正确的 - 但我只是反复检索一个对象的内容。我试过搞乱静态和非静态的重新分配,但这似乎只会产生更多的问题。 TestEmployee4依赖于文本文件,但问题肯定不在于我对文本的检索,因此它本质上是无关紧要的。 TestEmployee4还依赖于以前使用的类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: $1,000");

  }
 else
 {
 // do nothing  
 }



}
}

2 个答案:

答案 0 :(得分:3)

所以我想跟你讨论一些事情,有些事情已经过去了解你遇到的这个特殊问题。

首先,让我们解决手头的问题。 每当您打印Employees数组时,您都会执行以下代码:

public static void printEmployeeArray(ScottEmployee2[]employees)
{
    for(ScottEmployee2 i : employees){   
        ScottEmployee2.displayEmployee();

    }
}

此代码存在一些问题。首先,您在ScottEmployee2 []和员工之间没有空间。接下来,您在类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类中,你有几个方法作为静态,不应该是。事情是:

  • displayEmployee();
  • checkBonus();
  • 的setName(); //您不希望所有员工拥有相同的名称吗?
  • 的getName();

此外,该类中的几乎所有字段都不应该是静态的,因为它们不需要在类的每个实例化中保持一致。这意味着您的字段应如下所示:

private static int number; 
private int rank;
private String name,department,position; 
private double salary,percentage,modSalary;

静态的使用应该只是在所有类的实例中都有一个字段。拥有数字静态意味着当你创建一个新的ScottEmployee2时你所设置的任何东西都将是你所创建的下一个,除非你改变它。

我非常希望所有这些都能帮助您进行编码冒险! Pelase让我知道,如果还有什么我可以帮助你的!

答案 1 :(得分:1)

当您执行for / each循环时

ScottEmployee2.displayEmployee();

但是,您为ScottEmployee2对象分配变量i。请尝试改为:

i.displayEmployee();

还有一些(希望是有帮助的)有关您的代码的评论。

在你的一种方法中,你有类似的东西:

if(//something) {
   //Do something
}
else {
//do nothing
}

但是,每个if语句都不需要else。你可以这样做:

if(//something) {
  //do something
}

离开其他部分。此外,这仅适用于约定,但习惯上使用括号,如上面的代码,您在if语句的同一行上有开始括号。总的来说,一个非常简单的错误