There are some difficulties in learning Java .

Idea

Create a pet that will simply say what he wants in the most primitive way.

Code:

Main class

public class Progs { public static void main(String[] args){ Pet MyPet = new Pet(); MyPet.says(); } } 

Side effects

 public class Pet { String[] all_says = {"Я хочу кушать","Я хочу пить","Все отлично"}; public String says(String namePet, int needPet){ String nd; if(needPet == 1){ nd = all_says[1]; } else{ if(needPet == 2){ nd = all_says[2]; } else{ nd = all_says[3]; } } System.out.println(namePet + ":" + nd); } } public class Fish{ String name = "Karlson"; int need = 1; Pet MyPet = new Pet(); MyPet.says(name, need); } 

Mistake:

The Eclipse compiler produces the following error:

Exception in thread "java.lang.Error: Unresolved compilation of the problem: The method says (String, int) in the type of pet." At Progs.main (Progs.java.)

And yes, I know English and it says here that types are not applicable for arguments. But I do not understand what is wrong?

  • and what is the question then? if only this site catches a glimpse of if (needPet == 1) {String nd = all_says [1]; } else {if (needPet == 2) {String nd = all_says [2]; } else {String nd = all_says [3]; }} Why is there 3 times to declare one and the same variable? - Alexander Moshnov
  • I apologize, added a question - k0mar
  • @DreamChild, I'm afraid that it is) - k0mar
  • @fleg, corrected) - k0mar
  • I certainly understand that Pohpu and JS are very tolerant of the developer’s abilities and attentiveness ... but still calling the method with no arguments when he takes as many as two ... it’s time to try - DreamChild


2 answers 2

Of course, I’m not good at the pope, but I think that the top-starter needs to be hard on the head, that Java is a language that tightly controls the number of function / method parameters and not only the number of parameters, but also the type of exception thrown (if there is one) and return type. All this is collectively referred to as the method signature.

Now back to the question. As you have already correctly answered, the compiler swears at the discrepancy of the declared types to its call.

your method signature:

 public String Pet.says(String namePet, int needPet) 

And you try to call it as:

 Pet.says(); 

Accordingly, the compiler tries to find in the Pet class the method says() without parameters and naturally does not find it there and from there it swears.

    The compiler swears at MyPet.says (); In public static void main there should be two parameters of type String and int

    • @fleg, you can take an example, please :) - k0mar
    • Pet MyPet = new Pet (); MyPet.says ("Karlson", 2); - Alexander Moshnov 4:02 pm
    • public class Progs {public static void main (String [] args) {Pet MyPet = new Pet (); MyPet.says ("Karlson", 2); // was MyPet.says (); }} - Alexander Moshnov