The problem Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that the largest is 5 and the occurrence count for 5 is 4.
Breaking it down public static void main ( String [] strings ) {
Scanner input = new Scanner ( System . in );
System . out . print ( "Enter number: " );
String line = input . nextLine ();
input . close ();
IntStream numbersForMax = Arrays . stream ( line . split ( " " )). mapToInt (
Integer: : parseInt );
IntStream numbersForCount = Arrays . stream ( line . split ( " " )). mapToInt (
Integer: : parseInt );
int maxValue = numbersForMax . max (). getAsInt ();
long occurrence = numbersForCount . filter ( p -> p == maxValue ). count ();
System . out . println ( "The largest number is " + maxValue );
System . out . println ( "The occurrence count is " + occurrence );
}
Output Enter number: 1 2 3 4 5 6 6 6 6 6
The largest number is 6
The occurrence count is 5
Occurrence of max numbers posted by Justin Musgrove on 22 April 2016
Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch5
Share on: Facebook Google+