d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Am I Doing Wrong?
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 08:36pm
Quote (ferf @ Apr 23 2016 10:35pm)
oh yea, WOW i'm dumb LOL, sorry thanks! :D


advice for beginners: compile often. don't write all your code then compile when you're ready to run it. compile it every chance you get where you expect it to be syntactically correct. IDEs can compile every time you save and show you red squiggly lines to help guide you.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 08:38pm
Quote (ferf @ Apr 23 2016 10:36pm)
hmmm i still get these errors:


Code

Animal2.java:30: error: 'void' type not allowed here
System.out.println("Skunk is a " + skunk.check());


lets take a look at your check method:

Code
void check()


specifically, what is the return type? what does the return type mean?

now lets take a look at how you're using it.
Quote
System.out.println("Skunk is a " + skunk.check());


using your answer from earlier, what do you expect check() to return?
Member
Posts: 34,590
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 08:39pm
Quote (carteblanche @ Apr 23 2016 10:36pm)
advice for beginners: compile often. don't write all your code then compile when you're ready to run it. compile it every chance you get where you expect it to be syntactically correct. IDEs can compile every time you save and show you red squiggly lines to help guide you.


hmmm ok thanks

i stilll get these errors, not sure why:

Code
Code
C:\Users\ferfy\Desktop>javac Animal2.java
Animal2.java:28: error: 'void' type not allowed here
System.out.println("Dog is a " + dog.check());
^
Animal2.java:29: error: 'void' type not allowed here
System.out.println("tiger is a " + tiger.check());
^
Animal2.java:30: error: 'void' type not allowed here
System.out.println("Skunk is a " + skunk.check());





I mean, do i need to change return type on method "check"?
Member
Posts: 34,590
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 08:40pm
Quote (carteblanche @ Apr 23 2016 10:38pm)
lets take a look at your check method:

Code
void check()


specifically, what is the return type? what does the return type mean?

now lets take a look at how you're using it.


using your answer from earlier, what do you expect check() to return?


it returns a string? Not sure... System.out.println("etc");.... is string? idk
Member
Posts: 34,590
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 08:45pm
I changed to boolean for check method... (not sure if that's what i was supposed to do, but it seems to have fixed the error)

but i still get this error:

Code
Animal2.java:12: error: missing return statement
}
^



here is newest code:
Code
class Animal {
boolean omnivore;
boolean carnivore;
boolean herbavore;



boolean check() {
if(omnivore == true) System.out.println("omnivore");
if(carnivore == true) System.out.println("carnivore");
if(herbavore == true) System.out.println("herbavore");
}

}


class Animal2 {
public static void main(String args[]) {

Animal dog = new Animal();
Animal tiger = new Animal();
Animal skunk = new Animal();

dog.omnivore = true;
tiger.carnivore = true;
skunk.herbavore = true;

System.out.println("Dog is a " + dog.check());
System.out.println("tiger is a " + tiger.check());
System.out.println("Skunk is a " + skunk.check());


}
}


This post was edited by ferf on Apr 23 2016 08:48pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 09:24pm
Quote (ferf @ Apr 23 2016 10:45pm)
I changed to boolean for check method... (not sure if that's what i was supposed to do, but it seems to have fixed the error)

but i still get this error:

Code
Animal2.java:12: error: missing return statement
}
^



here is newest code:
Code
class Animal {
boolean omnivore;
boolean carnivore;
boolean herbavore;



boolean check() {
if(omnivore == true) System.out.println("omnivore");
if(carnivore == true) System.out.println("carnivore");
if(herbavore == true) System.out.println("herbavore");
}

}


class Animal2 {
public static void main(String args[]) {

Animal dog = new Animal();
Animal tiger = new Animal();
Animal skunk = new Animal();

dog.omnivore = true;
tiger.carnivore = true;
skunk.herbavore = true;

System.out.println("Dog is a " + dog.check());
System.out.println("tiger is a " + tiger.check());
System.out.println("Skunk is a " + skunk.check());


}
}


two things.

1) why did you choose boolean? do you really want a boolean here?
Quote
System.out.println("Dog is a " + dog.check());


2) you're saying the check method returns a boolean. where are you returning it? i dont see a return statement.
Code
boolean check() {
if(omnivore == true) System.out.println("omnivore");
if(carnivore == true) System.out.println("carnivore");
if(herbavore == true) System.out.println("herbavore");
}


hence the error "missing return statement"
Member
Posts: 34,590
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 09:27pm
Quote (carteblanche @ Apr 23 2016 11:24pm)
two things.

1) why did you choose boolean? do you really want a boolean here?


2) you're saying the check method returns a boolean. where are you returning it? i dont see a return statement.
Code
boolean check() {
if(omnivore == true) System.out.println("omnivore");
if(carnivore == true) System.out.println("carnivore");
if(herbavore == true) System.out.println("herbavore");
}


hence the error "missing return statement"


I have no idea...i'm not sure what to putf or the check method...
and riddles do not help me, i just need to know what i did wrong....

This post was edited by ferf on Apr 23 2016 09:28pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 09:28pm
Quote (ferf @ Apr 23 2016 11:27pm)
I have no idea...i'm not sure what to putf or the check method...


lets approach it from the other direction, then. instead of looking inside your check method, lets look at how you're using it.
Code
System.out.println("Dog is a " + dog.check());

what do you want that to print out when you run your code?
Member
Posts: 34,590
Joined: Mar 25 2009
Gold: 12,633.00
Apr 23 2016 09:31pm
Quote (carteblanche @ Apr 23 2016 11:28pm)
lets approach it from the other direction, then. instead of looking inside your check method, lets look at how you're using it.
Code
System.out.println("Dog is a " + dog.check());

what do you want that to print out when you run your code?


omnivore?

I've never covered strings though.. So would i use string for return value in check method?

like:

string check() {




not sure?



EDIT: Honestly i don't see why void doesn't work.....i mean i used void in similar code and it worked fine:

Code
void range() {
System.out.println("Range is: " + fuelcap * mpg);
}







EDIT: also i don't see why it would be boolean, as it's not returning a value of true or false, it's only checking if it's true or false in check method....not returning true or false..idk

This post was edited by ferf on Apr 23 2016 09:43pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2016 10:25pm
Quote
omnivore?

I've never covered strings though.. So would i use string for return value in check method?

Quote
EDIT: also i don't see why it would be boolean, as it's not returning a value of true or false, it's only checking if it's true or false in check method....not returning true or false..idk


sounds like a good start.

Quote
EDIT: Honestly i don't see why void doesn't work.....i mean i used void in similar code and it worked fine:


did you try calling it from System.out.println when you used it before? if not, then it's a totally different scenario.
Code
System.out.println(range());




This post was edited by carteblanche on Apr 23 2016 10:27pm
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll