2 Replies - 445 Views - Last Post: 23 April 2016 - 08:05 PM Rate Topic: -----

#1 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

What am i Doing wrong?

Posted 23 April 2016 - 07:32 PM

class Animal {
	boolean omnivore;
	boolean carnivore;
	boolean herbavore;
	
	
	
	void check() {
		if(omnivore == true) System.out.println("is an omnivore");
		if(carnivore == true) System.out.println("is an carnivore");
		if(herbavore == true) System.out.println("is an 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());
		
		
	}
}




Errors:
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());





Not sure what i'm doing wrong

This post has been edited by ferfykins: 23 April 2016 - 07:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: What am i Doing wrong?

#2 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: What am i Doing wrong?

Posted 23 April 2016 - 07:52 PM

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:

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



here is newest 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());
		
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3215
  • View blog
  • Posts: 12,098
  • Joined: 03-December 12

Re: What am i Doing wrong?

Posted 23 April 2016 - 08:05 PM

void and Boolean are identifiers in methods as to what they return. Void mean not return statement, Boolean means returns true or false.

As a general rule, you should include a scope identifier for class methods, hence the public void main. Public is the scope odentofier, void means returns nothing, main, the name of the method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1