I'm learning JAVA and trying to make a program that sums and averages an arbitrary number of integers. I've made two methods for this, one that takes the number of entries:
private Scanner takeNumberOfEntries(){
Scanner numberOfEntries = new Scanner(System.in);
System.out.println("Number of entries:");
while (!numberOfEntries.hasNextInt()){
System.out.println("Enter the right format");
numberOfEntries.next();
}
return numberOfEntries;
}
and passes it on to another that reads the inputs and stores them in an array:
private void summingAndAveraging(Scanner numberOfEntries){
int entryCountLimit = numberOfEntries.nextInt();
int[] inputArray = new int[entryCountLimit];
System.out.println("Enter the integer values");
for (int i = 0; i < entryCountLimit ; i++){
Scanner inputValues = new Scanner(System.in);
while (!inputValues.hasNextInt()){
System.out.println("fu k you, enter again:");
inputValues.next();
}
inputArray[i] = (int) inputValues.nextInt();
}
}
When I try to call it in main, I get errors. I've tried both:
takeNumberOfEntries instant = new takeNumberOfEntries();
and
Scanner instantiation = new takeNumberOfEntries();
I'm not sure what I'm doing wrong.
You cannot instantiate a function. You have to instantiate a class then call a function.
public class Class {
public void function() {
}
public static void main(String[] args) {
Class instance = new Class();
instance.function();
}
}