Reputation: 9
I am very new to Java and I am trying to make a chat bot. I have a main method then a method with the responses. Although when I try to call the response method in the main method, it highlights the word Response and says: method Response in class ChatCode cannot be applied to given types; required: java.lang.String; found: no arguments; reason: actual and formal argument lists differ in length
public class ChatCode{
public static String main(String input){
Scanner sc = new Scanner(System.in);
System.out.println("Hello!");
input = sc.nextLine();
while(input != ("Bye")){
Response();
}
System.out.println("Bye!");
}
Then this is my response method
public static String Response(String output){
Scanner sc = new Scanner(System.in);
input = input.toLowerCase();
input = input.trim();
String output;
if(input.indexOf("hi!") >= 0
|| ("hi") >= 0
|| ("hello!") >= 0
|| ("hello") >= 0){
output = "Hello!";
}
else if(input.indexOf("whats up?") >= 0
|| ("what's up?") >= 0
|| ("whats up") >= 0
|| ("what's up") >= 0){
output = "Nothing much, how about you?";
}
else if(input.indexOf("how are you?") >= 0
|| ("how are you") >= 0){
output = "I am great, how about you?";
}
return output;
}
Any feedback would be appreciated!!!!
Upvotes: 0
Views: 70
Reputation: 37059
You declared the Response()
method to require a single argument of type String
. That means that as it is written, you need to pass it one String
when you call it. That is what the compiler is telling you: It's enforcing a requirement you specified when you wrote the method. It's not communicating very effectively (compilers are inarticulate beasts), but now you know what that message means.
Since you never use the argument anyway inside the function (instead you're using a local variable of the same name), just remove it from the function definition and then you'll be fine calling Response()
with no arguments.
I hope you'll carefully read all of the answers; there are a few other problems with your code as well.
Upvotes: 1
Reputation: 3
That's because you declared the Response method as follows
public static String Response(String output){...}
When you are calling it, you need to follow the format with passing a argument. The argument type need to be "String".
Response(passAnyStringYouWantHere);
Upvotes: 0
Reputation: 169
There are many things that are incorrect in your program.
1. while(input != ("Bye")){
To compare 2 string objects, use the equals() method instead of using !=.
while(!("Bye").equals(input)){
2. You should have also received compilation error in the Response(String output) method, since you are also creating a variable of the same name inside the method.
3. if(input.indexOf("hi!") >= 0
|| ("hi") >= 0
|| ("hello!") >= 0
|| ("hello") >= 0){
output = "Hello!";
}
I am assuming you have written 'input.indexOf("hi") >= 0' instead of just '("hi") >= 0'.
4. Looking at the code, I don't think you need to pass String output as argument in the Response method. Just remove this argument and also add an else block which will set a default value for the output variable.
Upvotes: 1
Reputation: 22982
Response(String output)
method takes one argument and you are not passing any argument to this method, valid call to this method is Response("stringValue");
and not Response();
.
Moreover, as already stated in comment by (Pshemo
) input != ("Bye")
will compare references of String
and not value. You should use "Bye".equals(input)
or "Bye".equalsIgnoreCase(input)
to check input.
Upvotes: 1