I'm working on a simple java program that will create an array of 50 random numbers and display them, then will print what the biggest number is and where its' index would be. However, I keep getting an error that says "Constructor cannot be applied to given types". I know that this is an issue with the argument that the constructor asks for but I'm not sure how to fix it so I was hoping to get some advice.
For reference, here's the code I have so far. The issue is for the constructor being used on line 49.
class maxNumArray {
//define variables to store the max number and its' index
private static int max;
private static int maxIndex = 0;
private static int[] numArray;
private static int arrayLength;
private static int myArrayLength;
public void maxNumArray(int myArrayLength){
//create an integer array with a length of 100
arrayLength = myArrayLength;
//return arrayLength;
}
public static void makeArray(){
//create a for loop to repeat for the length of the list
numArray = new int [arrayLength];
max = numArray[0];
for (int i=0; i<numArray.length; i ){
//set index [i] to a random bumber between -50 and 50
numArray[i] = (int)(Math.random() *102 (-51));
//if the number at position [i] is greater than the current max number, set it to the max and change the index to match.
if (numArray[i]>max){
max = numArray[i];
maxIndex = i;
}
//print out the array
System.out.println(numArray[i] " ");
}
}
public static void maxValue(){
//print out the max value and its' index within the array
System.out.println("The highest value is " max " at an index of " maxIndex);
}
}
class Main{
public static void main(String[] args) {
maxNumArray myArray = new maxNumArray (100);
myArray.makeArray();
myArray.maxValue();
}
}
CodePudding user response:
You should always capitalize your class names, to distinguish between methods.
public void maxNumArray(int myArrayLength)
This appears to be an attempt at a constructor. Constructors have no return type as they cannot return any value. Remove void
to make this a valid constructor.
public class MaxNumArray {
public MaxNumArray(int arrayLength) {
// do class initialization stuff here
}
}