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

Java: Find max and minimum int in a binary file import java.util.*; import java.

ID: 3723565 • Letter: J

Question

Java: Find max and minimum int in a binary file

import java.util.*;

import java.io.*;

public class WriteBinFile {

public static void main(String[] args) {

FileOutputStream fos = null;

ObjectOutputStream oss= null;

try {

oss= new ObjectOutputStream(new FileOutputStream("ints.bin"));

oss.writeInt(22);

oss.writeInt(33);

oss.writeInt(-34);

oss.writeInt(109);

oss.writeInt(0);

oss.writeInt(-89);

oss.writeInt(4);

oss.close();

}

catch(FileNotFoundException e) {

System.out.println("File not Found.");

System.exit(0);

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

}

import java.util.*;

import java.io.*;

import java.text.DecimalFormat;

import java.text.NumberFormat;

public class ReadBinFile {

public static void main(String[] args) {

FileInputStream fis = null;

ObjectInputStream ois= null;

int x=1;

int largeInt=0;

int smallInt=0;

try {

ois= new ObjectInputStream(new FileInputStream("ints.bin"));

double avg;

int counter = 0;

int temp= ois.readInt();

while(true) {

counter++;

int number = ois.readInt();

if(number>temp) {

largeInt= number;

}

else {largeInt= temp;}

if(number<temp) {

smallInt= number;

}

else {smallInt= temp;}

temp= number;

}

}

catch(FileNotFoundException e) {

System.out.println("File not Found.");

System.exit(0);

} catch (IOException e) {

System.out.println("End of file-----");

}

NumberFormat formatter = new DecimalFormat("#0.00");

System.out.println("The largest number encountered: "+largeInt);

System.out.println("The smallest number encountered: "+smallInt);

}

}

Explanation / Answer

Hi

I have fixed the issues and highlighted the code changes below

ReadBinFile.java

import java.util.*;

import java.io.*;

import java.text.DecimalFormat;

import java.text.NumberFormat;

public class ReadBinFile {

public static void main(String[] args) {

FileInputStream fis = null;

ObjectInputStream ois= null;

int x=1;

int largeInt=0;

int smallInt=0;

try {

ois= new ObjectInputStream(new FileInputStream("D:\ints.bin"));

double avg;

int counter = 0;

largeInt= ois.readInt();

while(true) {

counter++;

int number = ois.readInt();

if(number>largeInt) {

largeInt= number;

}


if(number<smallInt) {

smallInt= number;

}
}
}

catch(FileNotFoundException e) {

System.out.println("File not Found.");

System.exit(0);

} catch (IOException e) {

System.out.println("End of file-----");

}

NumberFormat formatter = new DecimalFormat("#0.00");

System.out.println("The largest number encountered: "+largeInt);

System.out.println("The smallest number encountered: "+smallInt);

}

}

Output:

End of file-----
The largest number encountered: 109
The smallest number encountered: -89

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote