Home > Back-end >  Java - Cannot create object. Getting error message: "Object cannot be resolved to a type"
Java - Cannot create object. Getting error message: "Object cannot be resolved to a type"

Time:09-25

New programmer learning Java. I have an assignment where I have to create a database of sorts to store Consumer information. For now I've build two classes. The Consumer class itself, and a second, separate class with the main method. I'm still building the program, but I've run into something that I just can't seem to get past. My program won't let me create any objects of the class Consumer. Please help if anyone has a chance.

Consumer class code:

import java.lang.Enum;

public class Consumer {
    
    private String name;
    private int streetNumber;
    private String streetName;
    private String city;
    private String postalCode;
    private int age;
    private String gender;
    private static int numConsumers;
    

    
    public Consumer(String name) {
        numConsumers  ;
        this.name = name;
        this.streetNumber = 0;
        this.streetName = " ";
        this.city = " ";
        this.postalCode = " ";
        this.age = 0;
        this.gender = " ";
        MaritalStatus maritalStatus = MaritalStatus.SINGLE;
        Education education = Education.HIGH_SCHOOL;
    }
    
    public enum MaritalStatus {
        SINGLE,
        COMMON_LAW,
        MARRIED,
        SEPARATED,
        DIVORCED,
        WIDOWED
    }
    
    public enum Education {
        HIGH_SCHOOL,
        COLLEGE,
        UNIVERSITY,
        NONE
    }
    
    public void setName(String newName) {
        name = newName;
    }
    
    public String getName() {
        return name;
    }
    
    public void setStreetNumber (int n) {
        streetNumber = n;
    }
    
    public int getStreetNumber() {
        return streetNumber;
    }
    
    public void setStreetName(String newStreetName) {
        streetName = newStreetName;
    }
    
    public String getStreetName() {
        return streetName;
    }
    
    public void setCity(String newCity) {
        city = newCity;
    }
    
    public String getCity() {
        return city;
    }
    
    public void setPostalCode(String newPostalCode) {
        postalCode = newPostalCode;
    }
    
    public String getPostalCode() {
        return postalCode;
    }
    
    public void setAge (int n) {
        age = n;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setGender(String newGender) {
        gender = newGender;
    }
    
    public String getGender() {
        return gender;
    }
    
    public String toString() {
        System.out.println();
        System.out.println("Displaying information for consumer "   name);
        System.out.println("Address: "   streetNumber   " "   streetName   ", "   city   ", "   postalCode);
        System.out.println("Age: "   age);
        System.out.println("Gender: "   gender);
        System.out.println("Marital status: "   MaritalStatus.class);
        System.out.println("Education level: "   Education.class);
        return " ";
    }
    
    public static String menu() {
        System.out.println("What do you want to do?");
        System.out.println("\t 1. Enter a new Consumer (password required)");
        System.out.println("\t 2. Change all information of a Consumer (password required)");
        System.out.println("\t 3. Display all Consumers similar to a given consumer");
        System.out.println("\t 4. Display all Consumers with a given age and location");
        System.out.println("\t 5. Quit");
        System.out.println("Please enter your choice > ");
        return " ";
    }
    
    public static int getNumConsumers() {
        return numConsumers;
    }
    

}

and the code from my main class/method

import java.util.Scanner;

public class Capitalism {
    
    private static boolean validatedPassword;
    private static String correctPassword = "password";
    private static String passwordGuess;
    private static int passwordAttempts;
    private static int totalPasswordAttempts;
    private static int maxNumConsumers;
    private static int userMenuChoice;
    
    static int getMaxConsumers() {
        
        Scanner userInput = new Scanner(System.in); 
        System.out.println("Please enter the maximum number of consumers your company can handle");
        
        do {
            if(userInput.hasNextInt()) {
                maxNumConsumers = userInput.nextInt();
                if(maxNumConsumers <= 0) {
                    System.out.println("Invalid input. Please enter a positive number");
                }
            } else {
                System.out.println("Invalid input. Please enter a positive number (1, 8, 15, etc).");
                userInput.nextLine();
            }
        } while (maxNumConsumers <= 0);
        
        System.out.println("Your company can handle "   maxNumConsumers   " consumers.\n");
        return maxNumConsumers;
    }
    
    static void menu() {
        
        System.out.println("What do you want to do?");
        System.out.println("\t 1. Enter a new Consumer (password required)");
        System.out.println("\t 2. Change all information of a Consumer (password required)");
        System.out.println("\t 3. Display all Consumers similar to a given consumer");
        System.out.println("\t 4. Display all Consumers with a given age and location");
        System.out.println("\t 5. Quit");
        System.out.println("Please enter your choice > ");
        
    }
    
    static int getUserMenuChoice() {
        Scanner userInput = new Scanner(System.in); 
        
        do {
            if(userInput.hasNextInt()) {
                userMenuChoice = userInput.nextInt();
                if(userMenuChoice <= 0 || userMenuChoice >= 6) {
                    System.out.println("Invalid input. Please enter a number between 1 and 5.");
                }
            } else {
                System.out.println("Invalid input. User must enter a number with no letters (i.e. 4 instead of four).");
                userInput.nextLine();
            }
        } while (userMenuChoice <= 0 || userMenuChoice >=6);
        
        System.out.println("You have selected option "   userMenuChoice   ".\n");
        return userMenuChoice;
    }

    static void validatePassword() {
        while (!validatedPassword && passwordAttempts < 3) {
            System.out.println("Please enter your password.");
            Scanner userInput = new Scanner(System.in);
            passwordGuess = userInput.next();
            if (passwordGuess.equals(correctPassword)) {
                System.out.println("Correct password. Okay to proceed.");
                validatedPassword = true;
                break;
            } else if (passwordAttempts < 2) {
                System.out.println("Incorrect password. Max of 3 attempts before returning to main menu.");
                passwordAttempts  ;
                totalPasswordAttempts  ;
            } else {
                passwordAttempts = 0;
                totalPasswordAttempts  ;
                if (totalPasswordAttempts == 12) {
                    System.out.println("Program has detected suspicious activity and will terminate immediately.");
                    System.exit(0);
                }
                System.out.println("Maximum number of attempts reached. Returning to main menu.");
                break;
            }   
        }
    }
    
    static void validatePassword2() {
        while (!validatedPassword && passwordAttempts < 3) {
            System.out.println("Please enter your password.");
            Scanner userInput = new Scanner(System.in);
            passwordGuess = userInput.next();
            if (passwordGuess.equals(correctPassword)) {
                System.out.println("Correct password. Okay to proceed.");
                validatedPassword = true;
                break;
            } else if (passwordAttempts < 2) {
                System.out.println("Incorrect password. Max of 3 attempts.");
                passwordAttempts  ;
                totalPasswordAttempts  ;
            } else {
                System.out.println("Program terminated due to safety reasons.");
                System.exit(0);
            }   
        }
    }
    
    public static void main(String[] args) {
        
        Consumer consumerOne = new Consumer(); // this is the line where the error message comes up
        
        boolean runProgram = true;
        System.out.println("Welcome to the consumer information database. A place to store and update consumer information. \n");

        getMaxConsumers();
        
        while(runProgram) {
            menu();
            getUserMenuChoice();
            
            switch(userMenuChoice) {
            case 1:
                validatePassword();
                System.out.println("Then we continue the rest of this nonesense");
                break;
            case 2:
                validatePassword2();
                break;
            case 3:
            case 4:
            case 5:
                runProgram = false;
                break;
            }
        
        }
        
        System.out.println("Thank you for using the database. May all your capitalist dreams come true.");
        System.out.println("Program is now terminated.");

        }

    

}

CodePudding user response:

Your class Consumer has only one constructor with one required String parameter public Consumer(String name), but in main you pass nothing to constructor.

You should write

Consumer consumerOne = new Consumer("John");

CodePudding user response:

You are trying to create an instance of the class Consumer using the constructor Consumer() which is not defined.

The only constructor you have defined is Consumer(String name).

You need to add a default constructor to your Consumer class:

 public Consumer() {
    numConsumers  ;
    this.name = "Undefined"; //Or just set it to null
    this.streetNumber = 0;
    this.streetName = " ";
    this.city = " ";
    this.postalCode = " ";
    this.age = 0;
    this.gender = " ";
    MaritalStatus maritalStatus = MaritalStatus.SINGLE;
    Education education = Education.HIGH_SCHOOL;
}

CodePudding user response:

If you want that line of code to work - put in this empty constructor into your class:

public Consumer() {

}

For more information, see Java Constructors.

  • Related