I have just started programming in java, and I was trying some stuff out. I had written some code for creating my own array with x indexes that I could fill in as the program runs. So if I run the program I could say x = 5 and I would have 5 indexes to fill in (e.g. 5, 2, 7, 4 and 7). The program would then find the max value and print it. I was then wondering if I could have my program print the number of times my maxValue was in the array. In the example above it would be two. I just can't seem to find out how to do this though.
This is the code I have so far:
import java.util.*;
public class oefeningen {
static void maxValue(int[] newArray){//this method decides the largest number in the array
int result = newArray[0];
for (int i=1; i<newArray.length; i++){
if (newArray[i] > result){
result = newArray[i];
}
}
System.out.println("The largest number is: " +result);
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int x; //this is the main part of the array
System.out.println("Please enter size of array:");
x = keyboard.nextInt();
int[] newArray = new int[x];
for (int j=1; j<=x; j++){//this bit is used for manually entering numbers in the array
System.out.println("Please enter next value:");
newArray[j-1] = keyboard.nextInt();
}
maxValue(newArray);
}
}