
Write a Java program that prompts the user to enter an integer through the command line interface. The program will then compare the given integer with a list of integers from a file named data.txt. The program will keep count of the number of integers in the file that are smaller than, equals to, and larger than the integer given by the user. Lastly, display in the command line interface all the counters that represent the number integers in the file according to the earlier comparison. Use classes and methods from the java.io package for all input.

Java Number Example
import java.io.*;
import java.util.*;
class NumberExample{
public static void main(String[] args) throws Exception{
ArrayList list=new ArrayList();
int small=0,large=0,equal=0;
System.out.println("Enter number:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num=Integer.parseInt(br.readLine());
BufferedReader reader=new BufferedReader(new FileReader(new File("C:/numbers.txt")));
String st="";
while((st = reader.readLine())!= null) {
list.add(Integer.parseInt(st));
}
for(int i=0;i<list.size();i++){
if(Integer.parseInt(list.get(i).toString())>num){
large++;
}
if(Integer.parseInt(list.get(i).toString())==num){
equal++;
}
if(Integer.parseInt(list.get(i).toString())<num){
small++;
}
}
System.out.println("Number of integers that are smaller than "+num+": "+small);
System.out.println("Number of integers that are greater than "+num+": "+large);
System.out.println("Number of integers that are equal to "+num+": "+equal);
System.out.println("Total number of integers are: "+(small+large+equal));
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.