MaxNum
Home
My Work
Links
Photo Gallery
About Me

 

      /**

      ** Author: Walid Bahsow.

      ** E-mail: wabsnet@yahoo.com

      ** Thursday 20th of March, 2003

      **/

 

 

      /* This is a simple program which reads numbers from a file,
          finds the largest one and prints it into another file.
      */

      import java.io.*;

      class MaxNum {
          static StreamTokenizer in; // input file
          static PrintWriter out; // output file
          public static void main(String[] args)throws IOException{

          // A call for the printMax method and giving it
          // the maximum number which is returned by the metho findMax.

          printMax(findMax());

          } // end main

          // Finds the largest number in the file maxnum.in and returns it.   
         
          static int findMax()throws IOException {
              in = new StreamTokenizer(new FileReader("maxnum.in"));
              int max = Integer.MIN_VALUE;
              while (in.nextToken() != in.TT_EOF) {
                  if ((int) in.nval > max) {
                      max = (int) in.nval;
                  }
              }
              return max;
          }

          // Prints the integer passed to it in the file maxnum.out

          static void printMax(int x)throws IOException {
              out = new PrintWriter (new FileWriter ("maxnum.out"));
              out.println("The largest number is: "+x);
              out.close();
          }

      } // end class MaxNum

      Sample Input:

      8
                      2
      2
        1
      0
                                          5


      9

               258

      -897

      898


      -132

                                                              235




      17380

      Output:

      The largest number is: 17380.


 

 

Home | My Work | Links | Photo Gallery | About Me | Simple Java Programs | Sign Guest Book | View Guest Book

This site was last updated Friday March 21, 2003