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

1-Write a program that will search a file of numbers of type int and write the l

ID: 667868 • Letter: 1

Question

1-Write a program that will search a file of numbers of type int and write the largest and the smallest numbers to screen.The file to the screen.The file contains nothing but numbers of type int separated by blanks or line breaks.

2-Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends. The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the program. You can use your editor to enter the initial piece of advice in the file so that the first person who runs the program receives some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the Return Key Twice. Your program can then test to see that it has reached the end of the input by checking to see when it reads two consecutive occurrences of the character.

Explanation / Answer

1.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

   public static void main(String[] args) {

       BufferedReader br = null;
       int lar=0, sml=0;
       try {

           String sCurrentLine;

           br = new BufferedReader(new FileReader("C:\testing.txt"));

           while ((sCurrentLine = br.readLine()) != null) {
               if (sCurrentLine>lar)
               {
                   lar=sCurrentLine;
               }
               else if (sCurrentLine <sml)
               {
                   sml=sCurentLine;
               }
              
           }
           System.out.println("Largest Number is " +lar );
           System.out.println("Smallest Number is " +sml );

       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (br != null)br.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }

   }
}

2.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.*;

public class BufferedReaderExample {

   public static void main(String[] args) {

       try (BufferedReader br = new BufferedReader(new FileReader("C:\testing.txt")))
       {

           String sCurrentLine;

           while ((sCurrentLine = br.readLine()) != null) {
               System.out.println(sCurrentLine);
           }

       } catch (IOException e) {
           e.printStackTrace();
       }

       try {
           DataInputStream o = new DataInputStream(System.in);
           System.out.println("Write Your Advice");
           String content = o.readLine();file";

           File file = new File("c:/advice.txt");

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
           bw.write(content);
           bw.close();

           System.out.println("Done");

       } catch (IOException e) {
           e.printStackTrace();
       }

   }
}