What doesn't this code work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What doesn't this code work?

I've been trying to make a program with multiple user inputs, but I get a bunch of errors when I try to run it. The code is supposed to get the users input on what animal to name(dog or cat) and then based on that they will be able to either name a dog or a cat and get a message. Here's the code: import java.util.Scanner; public class Animal { public static void animal(){ Animal animal = new animal } public static void whichAnimal(){ Scanner dogorcat = new Scanner(System.in); String animal= dogorcat.nextLine(); } public static void catName(){ Scanner name = new Scanner(System.in); System.out.print("Meow! My name is "+ name.nextLine()+"."); Animal Cat = new Animal(); } public static void dogName(){ Scanner name = new Scanner(System.in); System.out.print("Bark! My name is "+ name.nextLine()+"."); Animal Dog = new Animal(); } public static void main(String[] args){ dogorcat.whichAnimal(); if(string animal = "cat"){ Cat.catName(); } else{ Dog.dogName(); } } }

24th Mar 2017, 8:08 PM
Emily
4 Answers
+ 17
Fixed code: import java.util.Scanner; public class Animal { String name; public void whichAnimal(){ Scanner input = new Scanner(System.in); String animal= input.nextLine(); name = animal; // not a good approach though } public void catName(){ System.out.print("Meow! My name is "+ name+"."); } public void dogName(){ System.out.print("Bark! My name is "+ name+"."); } public static void main(String[] args){ Animal dogorcat = new Animal(); dogorcat.whichAnimal(); if(dogorcat.name.equals("cat")){ dogorcat.catName(); } else{ dogorcat.dogName(); } } /* Please focus on how to create objects, how to call a class method, when to use static methods and so on */ }
24th Mar 2017, 9:54 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 15
I see lots of problems actually. Before solving complex problems, try to practice simple classes at first to understand OOP better.
24th Mar 2017, 9:42 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 1
There's far too many errors in this program which means you are a few steps out of touch of basic programming. That's ok! Just look at a few syntax issues. and start there. I quickly see these issues, (among others) 1. Animal animal = new animal // is making an object of Animal, not animal it would be., Animal animal = new Animal(); 2. you only need one instance of Scanner and it should be used in Main. Scanner input = new Scanner(System.in); and then whenever you want to assign input to a string etc, than you create a varriable like String name; name= input.nextLine(); // for example. 3. in the if statement, you can't create and assign variables ( String animal = "cat") is wrong. you must create a variable outside the statement. String animal; (apply the previous step to give animal a value via input) then do if(animal == "cat") // also, you need two " = " signs. one sign is for assigning values, while two == is for comparison. I won't get into the whole use of Cat.catName method stuff because i feel like the others stuff needs to be addressed first. Also, use comments! This helps others to understand what you are attempting to do and it also makes you understand what you are trying to do for yourself. Good luck and retry.
24th Mar 2017, 10:37 PM
Damian Depuy
Damian Depuy - avatar
0
Where did you define the variable 'dogorcat'?
24th Mar 2017, 8:50 PM
David Koplik
David Koplik - avatar