1 package Algorithms; 2 3 import java.util.ArrayList; 4 import java.util.Scanner; 5 6 public class MaxValue { 7 public static void main(String [] args){ 8 // Read some integers from standard input and store them in a list 9 ArrayList<Integer> numbers= readNumbersFromStandardInput(); 10 // Find the maximum value 11 12 Integer max= MaxValue(numbers); 13 // print it out 14 System.out.println("Max Value"+ max); 15 16 17 } 18 19 private static ArrayList<Integer> readNumbersFromStandardInput() { 20 // Store numbers 21 ArrayList<Integer>numbers= new ArrayList<>(); 22 23 //Prepare to read numbers 24 Scanner scanner= new Scanner(System.in); 25 System.out.println("Enter some positive integers"+ "followed by a negative number"); 26 27 // Read and store 28 Integer readnumbers= scanner.nextInt(); 29 30 while(readnumbers>0) { 31 numbers.add(readnumbers); 32 readnumbers = scanner.nextInt(); 33 } 34 return numbers; 35 } 36 37 private static Integer MaxValue(ArrayList<Integer>numbers) { 38 //Intial champion 39 Integer champion = numbers.get(0); 40 // Look at each element as the challanger, and if the challenger is > champion, then replace 41 42 for (Integer challanger : numbers) { 43 if (challanger > champion) { 44 champion = challanger; 45 } 46 } 47 return champion; 48 } 49 } 50 51