11. Using Methods: II - I get an error but the code is correct

class Dog{

int age;

public Dog(int dogsAge) {

age = dogsAge;

}

public void bark() {

System.out.println("Woof!");

}

public void run(int feet) {

System.out.println("Your dog ran " + feet + " feet!");

}
public void getAge(){
return age;
}

public static void main(String[] args) {

Dog spike = new Dog(7);
spike.bark();
spike.run(20);

}

}
This is my. I get error at the second instruction when I have to write “return age;” … What’s the problem?

2 Likes

Hi, @fosterman ,

The following function is defined as being void, meaning it does not return anything, but then it returns an int

public void getAge(){
  return age;
}

It should be …

public int getAge() {
1 Like

I’m having the same problem. I tried public int getAge( ) but that still does not seem to fix anything, it still says error.

If you could include a screen shot to show us what you are seeing, that will be helpful.

5 hour ago i had the same problem, later i used public int getAge(){} and works, later i restart all the java course, an d the same problem and dont work int getAge() i think it is a bug

yea thanks, it worked, but the problem is that the instruction told me to write it as void :slight_smile:

It does in Step 1, but then in Step 2 it says:

Modify the method so that it returns an int.

That is telling you to change void to int.

doesnt work
I have code
public void run(int feet) {

System.out.println("Your dog ran " + feet + " feet!");

}

public void getAge() {

//return age;

}

public static void main(String[] args) {

Dog spike = new Dog(7);
spike.bark();
spike.run(20);

spike.getAge();

answer is
Did you add the getAge method? Make sure to place it between the run and main methods.

Im having the same problem as these guys, this code is throwing an error cant figure out why

class Dog{

int age;

public Dog(int dogsAge) {

age = dogsAge;

}

public void bark() {

System.out.println("Woof!");

}

public int getrun(int feet) {
System.out.println("Your dog ran " + feet + “feet!”);

}

public static void main(String[] args) {

Dog spike = new Dog(7);
spike.bark();
spike.ran(20);

}

}

im also having problems on this one and i noticed the change from void to int straight away unless you mean to change the main method to int which i dont think the question suggests, anyway after much banging my head against a brick wall and wearing out my f5 button i got this code to work

class Dog{
int age;

public Dog(int dogsAge)
{
age = dogsAge;
}
public void bark(){
System.out.println(“Woof!”);
}
public void run(int feet){
System.out.println(“Your dog ran " + feet + " feet!”);
}
public int getAge(){
return age;
}

public static void main(String[] args) {

Dog spike = new Dog(5);
spike.bark();
spike.run(2000);
int spikeAge = spike.getAge();
}

}

but not everytime???

I made last code and result is same…not working…Actually i tried several times several different codes and …nothing

The exercise is expecting a method called run that is void.

Do you see the differences in your method and the expected one?

Hi @bitninja23259, if you post your code and error message, someone can have a look.

I see difference but thsi method works.
Ssytem tells that I dont have getAge method …but I have it in program…

even after help get the code it doesnt work…

I am having same problems…even after GET CODE …it is not properly fetched…I am unable to move for next assignment.

Not working, Anyone see the mistake?

It’s trying to tell you that it is surprised that you don’t have any more code after line 20.

Things are a bit cramped there, but it looks like you are missing a } – the one that matches this one:

class Dog {

solve my problem please :cry:

That was it, thanks Albion.

@official_mria I think you have the same problem as me. Add another } at the end.

1 Like