I am trying to output the amount of times the highest number appears in the user input for example user inputs 2 4 3 4 2 4 0 the highest number is 4 and it appears 3 times, not sure how to go about it.
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
String number, last;
System.out.println("Enter an interger (0 ends the input): ");
number = keyboard.nextLine();
last = number.substring(number.length() - 1);
while(!last.equals("0")){
System.out.println("Must end the input with a 0: ");
number = keyboard.nextLine();
last = number.substring(number.length() - 1);
}
String[] array = number.split(" ");
int max = Integer.MIN_VALUE, maxIndex = 0;
int count;
for (int i = 0; i < array.length; i++) {
if (Integer.parseInt(array[i]) > max) {
max = Integer.parseInt(array[i]);
maxIndex = i;
}
}
//String repeat = number.);
System.out.println("The largest number is " + max);
}
See Question&Answers more detail:os