previous | start | next

Example Use of MyIO Wrapper Methods.

Read a file integers (at most 100 integers), sort them and print the sorted integers to a file.

    1   
    2   public class ReadSort
    3   {
    4     
    5     public static void main(String[] args)
    6     {
    7       String infile;
    8       String outfile;
    9       final int MAXNUMS = 100;
   10   
   11       printDirections();
   12   
   13       Scanner stdin = new Scanner(System.in);
   14   
   15       System.out.print("Input file: ");
   16       infile = stdin.next();
   17   
   18       System.out.print("Output file: ");
   19       outfile = stdin.next();
   20   
   21       Scanner in = MyIO.openInput(infile);
   22       Scanner out = MyIO.openOutput(outfile);
   23   
   24       int[] nums = new int[MAXNUMS];
   25       int cnt = 0;
   26   
   27       while(in.hasNextInt() && cnt < MAXNUMS) {
   28         int x = in.nextInt();
   29         nums[cnt] = x;
   30         cnt++;
   31       }
   32   
   33       Arrays.sort(nums);
   34   
   35       for(int i = 0; i < nums; i++) {
   36         out.println(nums[i]);
   37       }
   38       out.close();
   39   
   40     }
   41   
   42   }


previous | start | next