evanparial
evanparial

Reputation: 45

Cannot be resolved to a type in VS Code [Java]

I am having problems running my Dog.class and DogDemo.java files in VS Code. I typed it exactly as written in a Java book. I am coming up with the error:

Dog cannot be resolved to a type

at DogDemo.main(DogDemo.java:7)

I also have tried cleaning the java workspace, but it doesn't work.

Dog.class

public class Dog
{
    public String name;
    public String breed;
    public age = 0;
    

    public void writeOutput()
    {
        System.out.println("Name " + name);
        System.out.println("Breed: " + breed);
        System.out.println("Age in calendar years" + age);
        System.out.println("Age in human years" + getAgeInHumanYears());
        System.out.println();
    }

    public int getAgeInHumanYears()
    {
        int humanAge = 0;

        if (age <= 2) 
        {
            humanAge = age * 11;
        }

        else
        {
            humanAge = 22 + ((age-2) * 5);
        }

        return humanAge;
    }
}

DogDemo.java

public class DogDemo 
{
    public static void main(String[] args) 
    {
        Dog kumo = new Dog();

        kumo.name = "kumo";
        kumo.age = 42;
        kumo.breed = "Corgi";
        kumo.writeOutput();

        Dog scooby = new Dog();
        scooby.name = "Scooby";
        scooby.age = 9;
        scooby.breed = "Great Dane";
        System.out.println(scooby.name + " is a " + scooby.breed +
        ".");`
        System.out.print("He is " + scooby.age + " years old, or ");

        int humanYears = scooby.getAgeInHumanYears();
        System.out.println(humanYears + " in human years.");
    }
}

Upvotes: 0

Views: 10558

Answers (3)

Sgh
Sgh

Reputation: 234

Dog

class Dog {
    String name;
    String breed;
    int age;

    void writeOutput()
    {
        System.out.println("Name " + name);
        System.out.println("Breed: " + breed);
        System.out.println("Age in calendar years" + age);
        System.out.println("Age in human years" + getAgeInHumanYears());
        System.out.println();
    }

    int getAgeInHumanYears()
    {
        int humanAge = 0;

        if (age <= 2)
        {
            humanAge = age * 11;
        }

        else
        {
            humanAge = 22 + ((age-2) * 5);
        }

        return humanAge;
    }
}

DogDemo

public class DogDemo {

    public static void main(String[] args) {
        Dog kumo = new Dog();

        kumo.name = "kumo";
        kumo.age = 42;
        kumo.breed = "Corgi";
        kumo.writeOutput();

        Dog scooby = new Dog();
        scooby.name = "Scooby";
        scooby.age = 9;
        scooby.breed = "Great Dane";
        System.out.println(scooby.name + " is a " + scooby.breed + ".");
        System.out.print("He is " + scooby.age + " years old, or ");

        int humanYears = scooby.getAgeInHumanYears();
        System.out.println(humanYears + " in human years.");
    }
}

The changes I made:

  1. removed public keyword from your data class Dog
  2. you missed ` on your main function where you are printing out name + breed
  3. add type int to the age in Dog's property

please also make sure your classes are in the same package or import it into your class if they are not.

EDIT:

I think other people also had the same issues as you are having you can find it here

And also you can follow the steps here, this documentation provided by VS code. You can see which steps are missing.

Upvotes: 0

Mark McElroy
Mark McElroy

Reputation: 373

you forgot to state the type of your variable age

    public age = 0;

should become

    public int age = 0;

In addition on DogDemo.java lines 17-18 you have

        System.out.println(scooby.name + " is a " + scooby.breed +
        ".");`

you have a backtick `, which isn't valid and should be removed to become

        System.out.println(scooby.name + " is a " + scooby.breed +
        ".");

Upvotes: 0

Andreas Rainer
Andreas Rainer

Reputation: 344

In your Dog-Class you forgot the type for the age variable (probably an integer), so correct it would be:

public class Dog
{
    public String name;
    public String breed;
    public int age = 0;

And in your DogDemo-class you've got a Typo in the line:

System.out.println(scooby.name + " is a " + scooby.breed +
    ".");``

The " ' " after the semicolon should be removed to:

System.out.println(scooby.name + " is a " + scooby.breed + ".");

Then it works just fine for me :) Keep on coding!

Upvotes: 2

Related Questions