简体   繁体   中英

Why do I get null as my output string instead of what I wanted?

I'm asked to compute a Java program with TestStaff that accepts name, staff id and working perday as inputs from the user and displays the name, staff ID and salary of the staff. But at the end of the output, I got null null 0.0 . I'm a beginner at this object and class section and I'm really lost. Can anyone help? Here are my codes:

This is the given code:

    class Staff {

       private String name, staffID;

       private double salary;

       private int workingDay;

       public void setStaffInfo(String nm, String id, int wDay){

          name=nm;
          staffID=id;
          workingDay=wDay;
    }

    public void calculateSalary(){

       salary = workingDay * 35.0;

       }
    public double getSalary(){

       return salary;

       }

    public String getName(){

       return name;

       }
    public String getStaffID(){

       return staffID;
       }

    }

    import java.util.Scanner;

Here are the codes for TestStaff:

    class TestStaff {

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

       Staff staff1= new Staff();

       System.out.print("Enter your name: ");
       String nm=console.nextLine();

       System.out.print("Enter your Staff ID: ");
       String id=console.nextLine();

       System.out.print("Enter your working days: ");
       int wDay=console.nextInt(); 

       System.out.println(staff1.getName() + "\t" + staff1.getStaffID()+ "\t\t" + staff1.getSalary());

       }
     }

You need to use the Staff class set method to initialize its members.

ie: staff1.setStaffInfo(nm, id, wDay);

What you've done is simply creating 3 new variables and assigning them values, but your Staff objects' members were not set.
Also you might want to call calculateSalary() before getSalary() because the former initializes the salary variable.

You are missing to call method staff1.setStaffInfo(nm, id, wDay);

I would suggest to have parameterized constructor with all the instance variables of class Staff, create instance for Staff using that constructor. That way you won't get null values. Suggestion: Create constructor in Staff class as:

Staff(){}

Staff(String name, String staffID, double salary ,  int workingDay){
    this.name = name;
    this.staffID=staffID;
    this.salary=salary;
    this.workingDay = workingDay;
}

And in TestStaff class, you can add these lines:

Staff staff2 = new Staff(nm,id, 2400, wDay);

    System.out.println(staff2.getName() + "\t" + staff2.getStaffID() + "\t\t" + staff2.getSalary());

But at the end of the output, I got null, null, 0.0.

You are not passing any arguments to the setStaffInfo(String nm, String id, int wDay) anywhere in your code so that is why it is showing null, null,0.0

Solution

Pass the following variables to the setStaffInfo(String nm, String id, int wDay) method of the class Staff

String nm=console.nextLine(); String id=console.nextLine(); int wDay=console.nextInt();

Like this:

staff1. setStaffInfo(nm,id,wDay)

And an advice

Run your calculateSalary() in getSalary() method like this:

public double getSalary(){
    calculateSalary(); //this will autimatically calculate salary
   return salary;

   }

Then, you will just have to call getSalary() in the main method and it will automatically call calculateSalary() or you can also put that one line of calculateSalary() in getSalary() also.

In the main method you have created the Staff object and stored all the input in some variable but haven't set the input in object. Thats why when you print staff1.getSalary() it returns you the default value of double ie: 0.0. first set the value staff1.setInfo(...) And then print the values.

You create a new object from your Class Staff but you have to assign data member to the class and can done from 2 different ways :

1- Your method staff1.setStaffInfo(nm, id, wDay);

staff1.setStaffInfo(nm, id, wDay);

2- Constructor

 Staff staff1= new Staff(id, nm, wDay);

Add the constructor below in Staff class this constructor will make you able to create a new object with specifying id, nm and wDay

Staff  (String staffID ,String name , int workingDay) {
this.staffID = staffID;
this.name = name ;
this.workingDay = workingDay ;
}

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