I'm trying to get maximum and minimum number from the file, but it shows me the last number on the file, not the actual max and min. Could you please help?
import java.util.Scanner; import java.io.*;
public class CalculateNumbers {
static void calculation(String fileName) throws IOException { int highest; int lowest; // Open the file File file = new File(fileName); Scanner inputFile = new Scanner(file); while (inputFile.hasNext()) { int num = inputFile.nextInt(); highest = num; lowest = num; if (num > highest) { highest = num; } if (num < lowest) { lowest = num; } } System.out.println("max num is " + highest); System.out.println("min num is " + lowest); inputFile.close(); } public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner (System.in); // Get the file name System.out.print("Enter the name of the file: "); String fileName = keyboard.nextLine(); calculation(fileName); https://stackoverflow.com/questions/66450655/cant-get-max-and-min-number-from-file-java March 03, 2021 at 12:07PM
没有评论:
发表评论