Why this doesn't work?

class Faculty extends Employee 
{
    private long[] officeHours;
    private String rank;

    public Faculty(long[] hours, String rank)
    {
        this.officeHours = hours;
        this.rank = rank;
    }

 public static void main(String[] args){


     //some code here

     Faculty f = new Faculty(newToken[], tokens[tokens.length-1]);

Recommended Answers

All 5 Replies

Maybe newToken is not an array of longs, but if it is, then certainly because tokens[tokens.length-1] isn't a String

Because of something missing in the //some code here part. You can do long[] newToken or long newToken[]to get an uninitialized array. long[] newToken[] would give you a multidimensional array. For instance:

long[] newToken = new long[] {123,456,789};
long oldToken[] = new long[] {987,654,321};
long[] midlifeToken[] = new long[2][2];
        midlifeToken[0][0] = 123;
        midlifeToken[0][1] = 234;
        midlifeToken[1][0] = 345;
        midlifeToken[1][1] = 456;


String[] tokens = new String[] { "token1", "token2", "token3" };

// newToken contains 123, 456 and 789
Faculty f = new Faculty(newToken, tokens[tokens.length - 1]);

// oldToken contains 987, 654 and 321
        f = new Faculty(oldToken, tokens[tokens.length - 1]);

// midlifeToken contains 123 and 234
        f = new Faculty(midlifeToken[0], tokens[tokens.length - 1]);

I have the updated code.

import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

class Person 
{
    private String name;
    private String address;
    private String phoneNum;
    private String emailAddress;

    public Person(){}

    public Person (String[] data)
    {
        this(data[0], data[1], data[2], data[3]);
    }

    public Person(String name, String address,
                    String phoneNum, String emailAddress)
    {
        this.name = name;
        this.address = address;
        this.phoneNum = phoneNum;
        this.emailAddress = emailAddress;
    }

    public String toString()
    {
        return "Person\tname: " + name 
                + "\taddress: " + address
                + "\tphoneNumber: " + phoneNum 
                + "\temailAddress: " + emailAddress;
    }

}

class Student extends Person 
{
    private String classStatus;

    public Student (String status)
    {
        this.classStatus = status;
    }

    public String toString()
    {
        return "Student classStatus: " + classStatus + "\n";
    }     
}

class Employee extends Person 
{
    private long dateHired;
    private String office;
    private double salary;

    public Employee(){
    }

    public Employee (String office, double sal, long date)
    {
        this.dateHired = date;
        this.office = office;
        this.salary = sal;
    }

    public String toString()
    {
        Date d = new Date(dateHired);
        return "Employee\toffice: " + office
                + "\tsalary: " + salary
                + "\tdateHired: " + d + "\n";
    }
} 

class Faculty extends Employee 
{
    private long[] officeHours;
    private String rank;

    public Faculty(long[] hours, String rank)
    {
        this.officeHours = hours;
        this.rank = rank;
    }

    @Override
    public String toString()
    {
        return "Faculty\tofficeHours: " //+ officeHours[0]
                    + "\trank: " + rank + "\n";
    }      

}

class Staff extends Employee{

    String title;

    public Staff(String title)
    {
        this.title = title;
    }
    public String toString()
    {
        return "Staff\ttitle: "+ title + "\n" + super.toString();
    }

}


public class Exercise10_2M {

    public static void main(String[] args){

        String data = "Student#junior;Person#Jane Smith#1000 Santa Lane#1-800-234-4567#jsmith@sb.edu;Exit#;";


        Scanner input = new Scanner(data);
        input.useDelimiter(";");

        long[] newToken;

        while(input.hasNext())
        {
            String line = input.next();
            String[] tokens = line.split("#");

            String typeOfPerson = tokens[0];
            switch(typeOfPerson)
            {
                case "Exit"     : System.exit(0); break;
                case "Person"   :
                    Person p = new Person(tokens[1], tokens[2], tokens[3], tokens[4]);
                    System.out.println(p);
                    break;
                case "Student"  :
                    Student s = new Student(tokens[1]);
                    System.out.println(s);
                    break;
                case "Employee" :
                    Employee e = new Employee(tokens[1], Double.parseDouble(tokens[2]), Long.parseLong(tokens[3]));
                    System.out.println(e);
                    break;
              case "Faculty"  :
                    System.out.print(Arrays.toString(tokens));
                    newToken = new long[tokens.length-1];

                    for(int i = 1; i < tokens.length - 1; i++)
                    {
                        newToken[i] = Long.parseLong(tokens[i]);
                    }
                    Faculty f = new Faculty(newToken[], tokens[tokens.length-1]);
                    break;

                case "Staff" :
                    Staff st = new Staff(tokens[1]);
                    System.out.println(st);
                    break;

            }

        }

    }


}

is this code working ? if not, it would be easier if you mentioned the error message you get.

Tell the error msg. It will help to sort it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.