Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.*;
  3.  
  4. public class Decode{
  5.     public static void main (String[] args) throws IOException{
  6.        
  7.             String eingabe = args[0];
  8.             String ausgabe = args[1];
  9.             int key = 0;
  10.             boolean makesSense = false;
  11.            
  12.            
  13.            
  14.             if (ausgabe.endsWith(".txt") == false)
  15.                 throw new BadFormatException();
  16.            
  17.         try{
  18.            
  19.             while (makesSense == false && key <= 26 ){
  20.            
  21.                 System.out.println("mache neue file");
  22.                 File entschlusselt = new File(ausgabe);
  23.                 FileOutputStream fos = new FileOutputStream(ausgabe);
  24.                 FileInputStream fis = new FileInputStream(eingabe);
  25.            
  26.            
  27.                 while (fis.available() > 0){
  28.                     fos.write(decodeChar((char)fis.read(), key));
  29.                 }
  30.                
  31.  
  32.                 if (understand(ausgabe) == true){
  33.                     makesSense = true;                         
  34.                 } else {
  35.                     System.out.println("erhöhe key");
  36.                     key = key + 1;
  37.                 }
  38.                
  39.                 fis.close();
  40.                 fos.close();
  41.                
  42.                 System.out.println("key:" + key);
  43.  
  44.            
  45.             }
  46.            
  47.             if (makesSense == true)
  48.                 System.out.println("Schlüssel war: " + key);
  49.             else
  50.                 System.out.println("Konnte nicht Entschlüsseln");
  51.                
  52.                
  53.  
  54.            
  55.            
  56.            
  57.         }catch(FileNotFoundException a) {
  58.             System.out.println("Datei nicht gefunden.");
  59.         }catch(ArrayIndexOutOfBoundsException b){
  60.             System.out.println("Falsche Anzahl von Eingaben.");
  61.         }catch(NumberFormatException d){
  62.             System.out.println("Falsche Eingabetypen.");
  63.         }                  
  64.     }
  65.    
  66.     private static char decodeChar(char c, int key) {
  67.         key = key * -1;
  68.         int cleanKey = key%26;
  69.         if(cleanKey < 0)
  70.                 cleanKey += 26;
  71.         if (c >= 'a' && c<='z') {
  72.         if (c + cleanKey > 'z')
  73.             return (char) (c + cleanKey - 26);
  74.         return (char) (c + cleanKey);
  75.        
  76.         } else return c;
  77.  
  78.     }
  79.    
  80.     private static boolean understand(String ausgabe)throws IOException{
  81.        
  82.         String[] haufig = {"die","der","und","in","zu","den","das","nicht","von","sie","ist","des","sich","mit","dem","dass","er","es","ein","ich","auf","so","eine","auch","als","an","nach","wie","im","fuer"};
  83.        
  84.         String s = "";
  85.         int worterAnzahl = 1;
  86.         int checkedWorter = 0;
  87.         char c;
  88.        
  89.         try{   
  90.             FileInputStream fis2 = new FileInputStream(ausgabe);
  91.            
  92.             while(fis2.available() >0){
  93.                 c = (char) fis2.read();
  94.                 if (c >= 'a' && c <= 'z' ){
  95.                     s = s + c;
  96.                 }
  97.                 if (c == ' '){
  98.                     for( int i = 0; i<haufig.length; i++){
  99.                         System.out.println("checke durch das array");
  100.                         if (s.equals(haufig[i])){
  101.                             System.out.println("habe wort erkannt");
  102.                             checkedWorter = checkedWorter + 1;
  103.                             break;
  104.                         }              
  105.                     }
  106.                     s = "";
  107.                     System.out.println("erhoehe wortanzahl");
  108.                     worterAnzahl += 1;
  109.                 }
  110.             }  
  111.                
  112.             System.out.println("string: " + s);
  113.                
  114.  
  115.                    
  116.                
  117.             fis2.close();
  118.        
  119.         }catch(FileNotFoundException a) {
  120.             System.out.println("Datei nicht gefunden.");
  121.         }catch(ArrayIndexOutOfBoundsException b){
  122.             System.out.println("Falsche Anzahl von Eingaben.");
  123.         }catch(NumberFormatException d){
  124.             System.out.println("Falsche Eingabetypen.");
  125.         }
  126.        
  127.         System.out.println("wortzahl: " + worterAnzahl);
  128.         System.out.println("checkedWorter: " + checkedWorter);
  129.        
  130.         if ( ((checkedWorter*100)/worterAnzahl) >= 29){
  131.             System.out.println("ende understand");
  132.             return true;
  133.         }else{
  134.             System.out.println("ende understand");
  135.             return false;
  136.         }  
  137.        
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement