Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.*; import java.io.*; public class MainMethodClassName { public

ID: 3533927 • Letter: I

Question

import java.util.*;
import java.io.*;

public class MainMethodClassName
{
public static void main(String args[])
{

Scanner readFile = null;

try

{

readFile = new Scanner(
new FileInputStream("numbers.txt"));
}


catch(FileNotFoundException e)
{

System.out.println("Sorry, this file could not be found.");
}

int max = readFile.nextInt();
int min = max;
while(readFile.hasNextInt());
{
int next = readFile.nextInt();
if(min > next)
min = next;

if(max < next)
max = next;
}

System.out.println("Maximum number:" + max);
System.out.println("Minimum number:" + min);
}
}

I have a text file located in the same folder with some various numbers, and it won't work. Thanks in advance.

Explanation / Answer

100% working code. Checked in eclipse

Mistakes u did.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Numbers {

public static void main(String[] args) {

try {

File file = new File("C:\temp umbers.txt");

Scanner input = new Scanner(file);

int count = 0;

int min = Integer.MAX_VALUE;

int max = Integer.MIN_VALUE;

while (input.hasNext()) {

int next = input.nextInt();

min = Math.min(min, next);   

// Update maximum   

max = Math.max(max, next);

}

input.close();

System.out.println("Smallest number "+min);

System.out.println("Largest number "+max);   

  

} catch (FileNotFoundException ex) {

System.out.println("Could not find the output file");

}

}

}