Reputation: 15
I'm trying to use a method from another class inside my main method and im getting an error. Its telling me that Method in class can not be applied to given types.
public class studentclass {
public void inputloop(String[] args){
Scanner scan = new Scanner(System.in);
String[][] student = new String[10][];
Double[][] results = new Double[10][];
Double[] total = new Double[10];
String tableln = "";
for(int index = 0; index < student.length; index++){
System.out.println("\nPlease enter student " + (index+1) + "'s details");
String userinput = scan.nextLine();
student[index] = userinput.split(",");
results[index] = new Double[4];
results[index][0] = Double.parseDouble(student[index][2]);
results[index][1] = Double.parseDouble(student[index][3]);
results[index][2] = Double.parseDouble(student[index][4]);
results[index][3] = Double.parseDouble(student[index][5]);
total[index] = (results[index][0]*0.1+results[index][1]*0.4+
results[index][2]*0.2+results[index][3]*0.3);
System.out.println("\nStudent name\tFAN \t\tResult1\tResult2\tResult3\tResult4\tTotal");
tableln = tableln + "\n" + student[index][0] + "\t" + student[index][1] + "\t"
+ results[index][0] + "\t" + results[index][1] + "\t" + results[index][2] + "\t"
+ results[index][3] + "\t" + total[index];
System.out.println(tableln);
}
}
and then in my main method I type this.
public static void main(String[] args) {
studentclass info = new studentclass();
info.inputloop();
}
It says "Method inputloop in class studentclass can not be applied to given types. required String[] found no arguements." Please help me. Thanks
Upvotes: 0
Views: 1455
Reputation: 5521
try like this
public static void main(String[] args) {
studentclass info = new studentclass();
String[] student ={"John", "Mark"};
info.inputloop(student);
}
}
or
you can change public void inputloop(String[] args) to public void inputloop() as the input argument is never used
Upvotes: 0
Reputation: 1499790
Your method declaration shows that it expects to be passed a reference to an array of strings:
public void inputloop(String[] args)
So you can only call the method in its current form if you pass in a value of type String[]
.
However, you're not actually using args
anywhere within the method, so I suggest you just change the declaration to:
public void inputloop()
I suggest you read the section on defining methods in the Java tutorial, or find a section about declaring (and calling) methods in a good introductory book on Java. If you don't currently have a book from which you're learning Java, I suggest you get hold of one very soon. While Stack Overflow is great for answering specific questions, it's not good for learning basic concepts - there's a lot more to declaring and calling methods than is present in any of the answers to this particular question, and learning all of it one little bit at a time is very inefficient.
Upvotes: 1
Reputation: 11577
you are trying to use inputloop
when the method should get array of strings.
i'm guessing the idea was to pass the command line args
to it. do:
public static void main(String[] args)
{
studentclass info = new studentclass();
info.inputloop(args);
}
Upvotes: 0
Reputation: 95948
The signature of your method is:
public void inputloop(String[] args)
You should pass an array of Strings to the method:
info.inputloop(someStringArray);
See this for more info:
The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
I don't see that you're using this parameter, so you can simply change your method signature to
public void inputloop()
And now it'll be a valid call as you did it.
Upvotes: 3