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

Write a program in JAVA (project) that asks the user to specify an input file an

ID: 3821080 • Letter: W

Question

Write a program in JAVA (project) that asks the user to specify an input file and output file. Have the program read each line in the input file to find the smallest number. Then write that number to the output file with two decimal places along with "<== smallest number". To be clear - if the input file contained the numbers 3, 74, 50, 35 and 22 then the output file would only contain: 3.00 <== smallest number Submit only the Java file - NOT the zipped Netbeans project. Your Java class should be named FindSmallest. Therefore your Java file will be named FindSmallest.java.

Explanation / Answer

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FindSmallest {
   public static void main(String[] args) throws FileNotFoundException {
       Scanner scan = new Scanner(new File("src/input.txt"));//read from a file input.txt
       int low= Integer.MAX_VALUE;// variable to store the lowest number, default value is the maximum integer value
       int value;
       while(scan.hasNextInt()){// check end of file
           value=scan.nextInt();//read each int from the input.txt file
           if(value<low){// if the value is is lesser than low then
               low=value;
           }
       }
       scan.close();//close the scanner.
       String twoDecimalPlaces=String.format("%.2f", (float) low);// converting the value to 2 decimal value
       PrintWriter out = new PrintWriter("src/output.txt");// opening the output.txt file
       out.println(twoDecimalPlaces+"<== smallest number");// wrinting the lowest value to the file
out.close();// close the print writer
   }
}


----output---------
//prints nothing in the console
a new file output.txt is created with the required output.
----output---------

Note: first we have to create a input.txt inside the same folder as the FindSmallest.java file

Contains of the file input.txt
3
74
50
35
22

Output.txt
3.00<== smallest number

Feel free to ask any question in case of any doubt. Happy to help. God bless you!!

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