Advertisement
TheSTRIG

Lab 10 - IO & Exception

Dec 12th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.00 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class HitungIP {
  5.     public static String path;
  6.        
  7.     public static int bacaBaris() throws IOException{
  8.         FileReader bacaFile = new FileReader(path);
  9.         BufferedReader bacaBuffer = new BufferedReader(bacaFile);
  10.        
  11.         String baris;
  12.         int jumlahBaris = 0;
  13.        
  14.         //looping baca baris
  15.         while ((baris = bacaBuffer.readLine()) != null){
  16.             jumlahBaris++;
  17.         }
  18.        
  19.         bacaBuffer.close();
  20.        
  21.         return jumlahBaris;
  22.     }
  23.    
  24.     public static String[] OpenFile() throws IOException{
  25.         FileReader fr = new FileReader(path);
  26.         BufferedReader textReader = new BufferedReader(fr);
  27.                
  28.         int jumlahBaris = bacaBaris();
  29.         String[] textData = new String[jumlahBaris];
  30.        
  31.         int i;
  32.        
  33.         for (i=0;i<jumlahBaris;i++){
  34.             textData[i] = textReader.readLine();
  35.         }
  36.        
  37.         textReader.close();
  38.         return textData;
  39.     }
  40.    
  41.     // Method untuk menghitung nilai IP
  42.     public static double hitungIP(double NilaiTotal, double SKS){
  43.         double nilaiIP = 0;
  44.        
  45.         try {
  46.             nilaiIP = NilaiTotal/SKS;
  47.         }
  48.         catch (Exception e) {
  49.         }
  50.         return nilaiIP;
  51.     }
  52.    
  53.     public static void CetakFile(List<String> content, String Tipe){   
  54.         File file = null;
  55.        
  56.         if (Tipe == "lulus") {
  57.             file = new File("D:/ddp/lulus.txt");
  58.         }
  59.         else if (Tipe == "tidakLulus"){
  60.             file = new File("D:/ddp/tidakLulus.txt");
  61.         }
  62.         else if (Tipe == "error"){
  63.             file = new File("D:/ddp/error.txt");
  64.         }
  65.  
  66.         System.out.println("");
  67.         System.out.println("Membuat file " + Tipe + ".txt");
  68.         System.out.println("...");
  69.        
  70.         try{
  71.             // kalau belum ada filenya, buat lah
  72.             if (!file.exists()) {
  73.                 file.createNewFile();
  74.             }
  75.            
  76.             FileWriter fw = new FileWriter(file.getAbsoluteFile());
  77.             BufferedWriter bw = new BufferedWriter(fw);
  78.            
  79.             for(int i=0;i<content.size();i++){
  80.                 bw.write(content.get(i));
  81.                 bw.newLine();
  82.             }
  83.            
  84.             bw.close();
  85.            
  86.             System.out.println("Berhasil!");
  87.         }
  88.         catch(IOException e){
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.    
  93.     public static boolean isNumeric(String str){  
  94.         try{  
  95.             double d = Double.parseDouble(str);  
  96.         }  
  97.         catch(NumberFormatException nfe){  
  98.             return false;  
  99.         }  
  100.         return true;  
  101.     }
  102.    
  103.     public static boolean CekError(String Tipe, String zNilaiTotal, String zSKS, double zIP){
  104.         //- Nilai total atau jumlah SKS bukan merupakan bilangan
  105.         //- Jumlah SKS melebihi 25 atau bukan bilangan bulat
  106.         //- Nilai IP melebihi 4 (Dipastikan input tidak negatif)
  107.        
  108.         boolean isError = true;
  109.        
  110.         if (Tipe == "NilaiTotalSKS"){
  111.             if (isNumeric(zNilaiTotal) == true) { //Nilai Total merupakan bilangan
  112.                 if (isNumeric(zSKS) == true) { //SKS merupakan bilangan                
  113.                     //if (zSKS.matches("[0-9]*")){ //SKS merupakan bilangan bulat
  114.                         if (Double.parseDouble(zNilaiTotal) >= 0){ //Nilai Total tidak boleh minus
  115.                             if (Double.parseDouble(zSKS) <= 25 && Double.parseDouble(zSKS) >= 0){ //SKS antara 0 s/d 25
  116.                                 isError = false;
  117.                                 //DEBUG System.out.println("bilangan");
  118.                             }
  119.                         }
  120.                     //}
  121.                 }
  122.             }          
  123.         }
  124.         else if (Tipe == "IP"){
  125.             if (zIP <= 4 && zIP > 0){
  126.                 isError = false;
  127.             }
  128.         }
  129.        
  130.         return isError;
  131.     }
  132.    
  133.     public static void main(String[] args){
  134.         String file_path = "D:/ddp/";
  135.         String file_name;
  136.         String inputFileName = ".txt";
  137.         String[] tahunAjaran;
  138.         int ajaranAwal = 0;
  139.         int ajaranAkhir = 0;
  140.         boolean isTahunAjaranValid = false;
  141.         String[] dataMahasiswa;
  142.         double IP = 0;
  143.         //String[] contentLulus = new String[];
  144.         List<String> contentLulus = new ArrayList<String>();
  145.         List<String> contentTidakLulus = new ArrayList<String>();
  146.         List<String> contentError = new ArrayList<String>();
  147.         boolean isError = false;
  148.        
  149.         try{
  150.             // Minta nama file input.
  151.             Scanner scan = new Scanner(System.in);
  152.             System.out.print("Input file: ");
  153.             inputFileName = scan.next();
  154.            
  155.             inputFileName = inputFileName + ".txt";
  156.             file_name = file_path + inputFileName;
  157.                                
  158.             // Buka file input.
  159.             path = file_name;
  160.             String[] aryLines = OpenFile();
  161.            
  162.            
  163.             for(int i=0;i<aryLines.length;i++){
  164.             //DEBUG System.out.println(aryLines[i]);
  165.            
  166.             }
  167.            
  168.             // Baca baris pertama yang berisi tahun ajaran.
  169.             /* DEBUG
  170.             System.out.println("");
  171.             System.out.println("Tahun Ajaran: " + aryLines[0]);
  172.             */
  173.             tahunAjaran = aryLines[0].split("/");
  174.            
  175.             // Lakukan validasi tahun ajaran
  176.             ajaranAwal = Integer.parseInt(tahunAjaran[0]);
  177.             ajaranAkhir = Integer.parseInt(tahunAjaran[1]);
  178.            
  179.             if ((ajaranAkhir-ajaranAwal) == 1){
  180.                 //DEBUG System.out.println("Tahun Ajaran valid...");
  181.                 isTahunAjaranValid = true;
  182.             }
  183.             else  {
  184.                 System.out.println("Tahun Ajaran tidak valid...");
  185.             }
  186.                      
  187.             // throw exception anda sendiri kalau terjadi error
  188.             // while loop untuk memproses data dari file
  189.            
  190.             if (isTahunAjaranValid == true){
  191.                 //DEBUG System.out.println("");
  192.                 int iDetail=1;
  193.                 int iLulus = 1;
  194.                 int iGagal = 1;
  195.                 int iError = 1;
  196.                 contentLulus.clear();
  197.                 contentTidakLulus.clear();
  198.                 contentError.clear();
  199.                
  200.                 while (iDetail<aryLines.length){
  201.                     //DEBUG System.out.println("");
  202.                     dataMahasiswa = aryLines[iDetail].split(" ");
  203.                    
  204.                     for (int i=0;i< dataMahasiswa.length;i++){
  205.                         //DEBUG System.out.println(dataMahasiswa[i]);
  206.                     }
  207.                    
  208.                     isError = CekError("NilaiTotalSKS", dataMahasiswa[1], dataMahasiswa[2], 0);
  209.                    
  210.                     if(isError == false){
  211.                         IP = hitungIP(Double.parseDouble(dataMahasiswa[1]),Integer.parseInt(dataMahasiswa[2]));
  212.                    
  213.                         isError = CekError("IP", "", "", IP);
  214.                         if(isError == false){
  215.                             if (IP >= 1.75){
  216.                                 //DEBUG System.out.println("lulus");
  217.                                 contentLulus.add(aryLines[iDetail]);
  218.                                 iLulus++;
  219.                             }
  220.                             else if(IP < 1.75){
  221.                             //DEBUG System.out.println("tidak lulus");
  222.                             contentTidakLulus.add(aryLines[iDetail]);
  223.                             iGagal++;
  224.                             }
  225.                         }
  226.                         else if (isError == true){
  227.                             System.out.println("error IP");
  228.                             contentError.add(aryLines[iDetail]);
  229.                             iError++;
  230.                         }
  231.                     }
  232.                     else if (isError == true){
  233.                         System.out.println("Telah terjadi error!");
  234.                         contentError.add(aryLines[iDetail]);
  235.                         iError++;
  236.                     }
  237.                    
  238.                     iDetail++;
  239.                 }
  240.                
  241.                 if (iLulus>1){
  242.                     CetakFile(contentLulus, "lulus");
  243.                 }
  244.                
  245.                 if (iGagal>1){
  246.                     CetakFile(contentTidakLulus, "tidakLulus");
  247.                 }
  248.                
  249.                 if (iError>1){
  250.                     CetakFile(contentError, "error");
  251.                 }
  252.             }
  253.         }
  254.         catch (IOException e) {
  255.             System.out.println("");
  256.             System.out.println(e.getMessage());
  257.             System.out.println("File Not Found");
  258.             System.out.println("Pastikan file " + inputFileName + " ada di directory D:\ddp");
  259.         }
  260.        
  261.         catch (ArrayIndexOutOfBoundsException AIOoBE) {
  262.             System.out.println("tidak ada sks");
  263.         }
  264.        
  265.         catch (NumberFormatException NFE ) {
  266.             System.out.println("sks pake koma");
  267.         }
  268.         //catch (. . . ) {
  269.         //}
  270.         //}
  271.         }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement