Advertisement
nguyenvanquan7826

Array

May 16th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8. class InitArray
  9. {
  10.     int [] Element = new int[100];
  11.     int n;
  12. }
  13. class IOArray extends InitArray
  14. {
  15.     void input_file() throws FileNotFoundException
  16.     {
  17.         FileInputStream fi = new FileInputStream("input.txt");
  18.         Scanner inp = new Scanner(fi,"UTF-8");
  19.         String temp = inp.nextLine(); //doc dong mang trong file
  20.         String item[] = temp.split(" "); //tach chuoi thanh cac phan tu chuoi
  21.         n = item.length;
  22.         for(int i=0; i<n; i++) //doi kiem string sang int cua cac phan tu
  23.             Element[i] = Integer.parseInt(item[i]);
  24.         inp.close();
  25.     }
  26.     void output_file() throws IOException
  27.     {
  28.         FileOutputStream fo = new FileOutputStream("output.txt");
  29.         PrintWriter out = new PrintWriter(fo);
  30.         for (int i=0; i<n; i++)
  31.             out.printf("%-5d",Element[i]);
  32.         out.close();
  33.     }
  34. }
  35.  
  36. class SortArray extends IOArray
  37. {
  38.     public void soft()
  39.     {
  40.         Arrays.sort(Element);
  41.     }
  42.    
  43.     public void show()
  44.     {
  45.         for(int i=0; i<n; i++)
  46.         {
  47.             System.out.print(Element[i]+" ");
  48.         }
  49.     }
  50. }
  51.  
  52. class Array
  53. {
  54.     public static void main(String[] args) throws IOException
  55.     {
  56.         //IOArray Arr = new IOArray();
  57.         SortArray Arr = new SortArray();
  58.         Arr.input_file();
  59.         Arr.soft();
  60.         Arr.show();
  61.         Arr.output_file();
  62.         System.out.println("Succes ! Open file output.txt to view");
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement