koodeta
koodeta

Reputation: 83

Passing strings between classes and input being skipped

So the problem I have is that I am asking for a person's name in a class. The function then returns the string to be used by getName(). The getName() function is to be used in the Employee class. The primary problem is that the name of the person isn't being passed to the getName() function when it's called. The secondary problem is that the output question for "What is the name of the person" is being skipped. The program doesn't wait for the user to input any information, even though it's explicitly stated. Unless I'm supposed to put the data I'm looking for in the main() function to be passed to the Employee class I'm not sure what to do.

This is the Person class.

import java.util.Scanner;

public class Person {

    public static void main(String[] args) {

    }

    public Person(){
    }

    //asks for the name of the person and then returns the personName for the getName function to use it
    public static String setName(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the name of the person? ");
        String personName = input.toString();
        return personName;
    }

    public String getName(String personName){//returns the person name to be used with an Employee object
        return personName;
    }

}

This is the Employee class.

import java.util.Scanner;

public class Employee extends Person {

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

        person.setName();//calls to the person class (because an Employee object extends the parent class)

        System.out.println("What is the salary? ");//asks for salary
        double annualSalary = input.nextDouble();

        System.out.println("What is the starting year? ");//ask for the starting year
        int startYear = input.nextInt();

        System.out.println("What is the insurance number? ");//ask for insurance number
        String insuranceNum = input.toString();

        System.out.println("Name: " + person.getName(person.setName()) + //intent is to retrieve the name of the person object
                           "\nSalary: " + emp.getAnnualSalary(annualSalary) + 
                           "\nStart Year: " + emp.getStartYear(startYear) + 
                           "\nInsurance Number: " + emp.getInsuranceNum(insuranceNum));
    }

    //constructor
    public Employee(){
    }

    //returns the salary of the employee
    public double getAnnualSalary(double annualSalary){
        return annualSalary;
    }

    //returns the year the employee started
    public int getStartYear(int startYear){
        return startYear;
    }

    //returns the insurance number of the employee
    public String getInsuranceNum(String insuranceNum){
        return insuranceNum;
    }
}

Upvotes: 1

Views: 123

Answers (2)

Will_61
Will_61

Reputation: 116

  1. I think you're misunderstanding what getters and setters are usually used for. They normally are used to let another class get access to the data stored in the Person class.

A getName method shouldn't be given any parameters, and return the persons name:

public String getName()
{
  return personName;
}

The setName method should normally be passed a value to set personName to:

public void setName(String givenName)
{
  personName = givenName;
}

At the moment the class Person doesn't store the name anywhere... You need to make personName global - put the decleration outside of the setName method. eg:

public class Person()
{
  private String personName;
  // constructor, you could give a person an initial name
  public Person(String name) 
  {
    personName = name;
  } 
  // setter/getter methods below
  // ...
  //end of setter/getter methods
}

2. As codeNinja said, use input.nextLine(); to get input from user.

Upvotes: 2

libik
libik

Reputation: 23029

You have to use this String personName = input.nextLine(); instead of String personName = input.toString();


+very good advice - remove static from setName method.

Upvotes: 1

Related Questions