Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance program

 
Diana Aldueza
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does:
if(Type='d'){
System.out.println("Please enter the Furlength");
String FurLength = Keyboard.readString();
System.out.println("Please enter the Size of the dog");
String Size = Keyboard.readString();
pets [petCount] = new Dog(Name, Sex, Age, FurLength, Size);
}
else if(Type='c'){
System.out.println("Please enter the FurColor of the Cat");
String FurColor = Keyboard.readString();
System.out.println("Please enter the EyeColor of the Cat");
String EyeColor = Keyboard.readString();
pets [petCount] = new Cat(Name, Sex, Age, FurColor, EyeColor);
}
else if(Type='b'){
System.out.println("Please enter the FeatherColor");
String FeatherColor = Keyboard.readString();
System.out.println("Please enter the BeekColor of the Bird");
String BeekColor = Keyboard.readString();
pets [petCount] = new Bird(Name, Sex, Age, FeatherColor, BeekColor);
}
else{
System.out.println("You have entered the wrong option!");
}
not work. The error I get says:
found:java.lang.String
required:boolean
if(Type='d')
but I tried if(Type=='d')
and it still wont work. Can anyone help me find out whats wrong.
Thanks
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a declaration for the variable "Type"; what is it?
 
Larry Jones
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like Type is a String. To compare strings you need to use the equals() method.
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(Type='d') is an assignment, so like you figured, you need a boolean statement - if(Type == 'd') is a boolean statement.
Try
if("d".equals(Type)){
...
}
Try changing, recompiling, and testing again.

You said you changed = to == .

You got an error that says (Type='d') when you program uses (Type == 'd') -which is probably not what you want if Type is a String
means that you're running the wrong program or forgot to compile.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that 'd' is a char, while "d" is a String. These are not at all the same thing!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic