简体   繁体   中英

I am having trouble defining my constructor class

I am trying to call a method from another class into my main class, and it is telling me that the constructor I am using is undefined. Any suggestions for fixing? possibly what else I am doing wrong?

Main Class (Email)

package EmailApp;

public class Email {
    public static void main(String[] args) {
        EmailApp Email1 = new EmailApp();
        Email1.setFullName();
    }
}

Public Class(EmailApp)

package EmailApp;

import java.util.Scanner;

public class EmailApp {
    String firstName;
    String lastName;
    String password;
    String department;
    int mailboxCapacity;
    int defaultPasswordLength = 10;
    String alternateEmail;
    //Declaration of all objects

    public EmailApp(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;

        this.department = setDepartment();
        System.out.println("Department: " + this.department);
        // Printing the Department Name

        this.password = randomPassword(defaultPasswordLength);
        System.out.println("Your Password Is: " + this.password);
        //Printing Password generation results

        this.firstName = setFullName();
        this.lastName = setFullName();

    }
    //Entering the First and Last Name for Email Creation here

    public String setFullName() {
        Scanner firstlastscanner = new Scanner(System.in);
        System.out.println("Enter your first name: ");
        this.firstName = firstlastscanner.nextLine();
        System.out.println("Enter your last name; ");
        this.lastName = firstlastscanner.nextLine();
        firstlastscanner.close();

        System.out.println("Email Created: " + this.firstName + " " + this.lastName);
        //Entering the first and last name with a scanner
        return setFullName();
    }

    private String setDepartment() {
        System.out
                .print("Department Codes\n1 for Sales\n2 for Development\n3 for Accounting\n0 for None\nEnter the Department Code:");
        Scanner in = new Scanner(System.in);
        int depChoice = in.nextInt();
        if (depChoice == 1) {
            return "Sales";
        } else if (depChoice == 2) {
            return "Development";
        } else if (depChoice == 3) {
            return "Accounting";
        } else {
            return " ";
        }
        //Setting parameters for department codes and scanner for input of code
    }

    private String randomPassword(int length) {
        String passwordSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
        char[] password = new char[length];
        for (int i = 0; i < length; i++) {
            int rand = (int) (Math.random() * passwordSet.length());
            password[i] = passwordSet.charAt(rand);
            //Password Generation
        }
        return new String(password);
    }
}

EmailApp Email1 = new EmailApp();

Your constructor is public EmailApp(String firstName, String lastName) . As it is obvious, you do not pass any arguments to it. Its usage should be something like:

EmailApp Email1 = new EmailApp("John", "Doe");

or create a non argument constructor:

public EmailApp()
{
    //Do stuff without arguments
}

Also, take a look at Why do we need private variables?

It is giving this error because you have one parameterized constructor in EmailApp class. public EmailApp(String firstName, String lastName)

And yout are trying to access default constructor. that's why it is giving this error.

You need to declare default constructor like below in the EmailApp class and it will not give the Error:

public EmailApp() {

    }

OR use the parameterized constructor you have declared in the class like :

 EmailApp email1 = new EmailApp("james","gosling");
 email1.setFullName();

Whenever we created any java class, constructor for that particular class will already available, as the construction will be done by compiler itself.

But whenever you define your custom constructor, at that time default constructor won't be available for you...

For example you have class

Your defined class :

Public class EmailApp { }

After compilation:

public class EmailApp {

public EmailAp(){ }

}

So default constructor will be created by compiler.

But you are defining your own constructor so compiler don't need to create default one.

Remember always one constructor must require for any class in java.

You're EmailApp 's constructor takes two parameters (String firstName, String lastName), while you are constructing EmailApp and passing 0 parameters.

public class Email {
    public static void main(String[] args) {
        /* These are just example names */
        EmailApp Email1 = new EmailApp("Billy", "Bob");
        Email1.setFullName();
    }
}

Doesnt the setfullName Method crush your code as it runs itself the whole time

  public String setFullName() {
    Scanner firstlastscanner = new Scanner(System.in);
    System.out.println("Enter your first name: ");
    this.firstName = firstlastscanner.nextLine();
    System.out.println("Enter your last name; ");
    this.lastName = firstlastscanner.nextLine();
    firstlastscanner.close();

    System.out.println("Email Created: " + this.firstName + " " + this.lastName);
    //Entering the first and last name with a scanner
    return setFullName();//runs itself as return so it will run itself all the time.
}

You should either let the method return something else or let it return void.

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