简体   繁体   中英

Using toString() method in Java

I am trying to utilize the toString() in a class I have called Employee(). I have a 1D array of type Employee, which stores Employee Data, which include the Employee ID, Employee Name, Employee Address, and Employee Hire Date. I want the user to specify the amount of employees, and then enter the relevant data for however many employees the user wants. I then want to print the result for the user with the information entered. I keep getting some results that are null. I tried using an If statement that printed output if it didn't equal null, but that didn't work. I know the output works if I print out a single variable, such as address, but I want all variables to print out. Thank you for any help.

public class Address
{
  public String address;

  public void getAddress(String a)
  {
      address = a;
  }

  public String toString()
  {
      return address;
  }

}
public class Name
{
  String name;

   public void getName(String n)
  {
      name = n;
  }

  public String toString()
  {
      return name;
  }

}
public class Date
{

    public String date;
    public String date1;


    public void getDate(int d, int m, int y)
    {

        date1 = (m + "/" + d + "/" + y);

    }    

    public String toString()
    {
        return date1;
    }

}

import java.util.Scanner;

public class Employee
{
    private int number;
    private Name name1 = new Name();
    private Address address1 = new Address();
    private Date hireDate = new Date();
    String number1;

    public void getDate1(int d, int m, int y)
    {
        hireDate.getDate(d, m, y);
    }

    public void getID(int x)
    {
        number = x;


    }

    public void setName( String n)
    {
        name1.getName(n);
    }

    public void getAddress(String a)
    {
         address1.address = a;
    }

    String z;

    public String toString()
    { 

        number1 = String.valueOf(number);
        String name2 = String.valueOf(name1);
        String address2 = String.valueOf(address1);
        String hireDate2 = String.valueOf(hireDate);

        z = number1 + " " + name2 + " " + address2 + " " + hireDate2;
        return z; 
    }


    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter amount of Employees: ");
        int input1 = input.nextInt();
        input.nextLine();

        for (int i = 0; i < input1; i++)
        {
            Employee [] employees = new Employee[4];

            System.out.println("Please enter the employee ID: ");
            int employeeID = input.nextInt();
            input.nextLine();
            employees[0] = new Employee();
            employees[0].getID(employeeID);

            System.out.println("Please enter the employees Name: ");
            String name2 = input.nextLine();
            employees[1] = new Employee();
            employees[1].setName(name2);

            System.out.println("Please enter the employee's address: ");
            String address2 = input.nextLine();
            employees[2] = new Employee();
            employees[2].getAddress(address2);

            System.out.println("please enter hire date, day (1-31),");
            System.out.print("month (1-12), year (1901 - 2019) in order on seperate");
            System.out.print(" lines: ");
            int input2 = input.nextInt();
            int input3 = input.nextInt();
            int input4 = input.nextInt();

            employees[3] = new Employee();
            employees[3].getDate1(input2, input3, input4);

            for (int p = 0; p < employees.length; p++)
            {
                System.out.println(employees[p]);
            }

        }

    }  
}
I have create classes which were needed. You can change them but you are creating employee object every time. I have corrected the code debug it and you will know what was wrong.

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

class Name {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

public class Employee {

    private int number;
    private Name name1 = new Name();
    private Address address1 = new Address();
    private Date hireDate = new Date();
    String number1;

    public void getDate1(int d, int m, int y) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(y, m, d);
        hireDate = calendar.getTime();
    }

    public void getID(int x) {
        number = x;

    }

    public void setName(String n) {
        name1.setName(n);
    }

    public void getAddress(String a) {
        address1.setAddress(a);
    }

    String z;

    public String toString() {

        number1 = String.valueOf(number);
        String name2 = name1.getName();
        String address2 = address1.getAddress();
        String hireDate2 = String.valueOf(hireDate);

        z = number1 + " " + name2 + " " + address2 + " " + hireDate2;
        return z;
    }

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter amount of Employees: ");
        int input1 = input.nextInt();
        input.nextLine();

        Employee[] employees = new Employee[input1];
        for (int i = 0; i < input1; i++) {

            System.out.println("Please enter the employee ID: ");
            int employeeID = input.nextInt();
            input.nextLine();
            employees[i] = new Employee();
            employees[i].getID(employeeID);

            System.out.println("Please enter the employees Name: ");
            String name2 = input.nextLine();
            employees[i].setName(name2);

            System.out.println("Please enter the employee's address: ");
            String address2 = input.nextLine();
            employees[i].getAddress(address2);

            System.out.println("please enter hire date, day (1-31),");
            System.out.print("month (1-12), year (1901 - 2019) in order on seperate");
            System.out.print(" lines: ");
            int input2 = input.nextInt();
            int input3 = input.nextInt();
            int input4 = input.nextInt();

            employees[i].getDate1(input2, input3, input4);

            System.out.println(employees[i]);

        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM