Znajdź maksymalną wartość w Javie z wejścia pliku
Jestem nowym użytkownikiem Java i próbuję napisać program, który prosi użytkownika o podanie nazwy pliku txt zawierającego tylko liczby, a program wyświetli sumę, średnią, max i min liczb w pliku . Napisałem większość programu, ale utknąłem, próbując znaleźć maksymalne i minimalne wartości. Wszelkie informacje, które możesz podać, byłyby pomocne, a jeśli nie byłbym wystarczająco jasny, mogę spróbować je rozwinąć. Mój dotychczasowy kod to:
public class NumberFile{
public static void main(String[] args){
boolean goodName = false;
int currentNumber, sum = 0, numberCount=0;
Scanner numberFile = null;
FileReader infile;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the file you wish to import: ");
String fileName = input.nextLine();
while (!goodName){
try{
infile = new FileReader(fileName);
numberFile = new Scanner(infile);
goodName = true;
}
catch (IOException e){
System.out.println("invalid file name, please enter another name");
fileName = input.nextLine();
}
}
while (numberFile.hasNextInt()){
currentNumber = numberFile.nextInt();
sum+=currentNumber;
numberCount++;
}
System.out.println("The sum of the numbers is " +sum);
System.out.println("The average of the numbers is " + ((double) sum)/numberCount);
} // end main
} // end class