Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.lang.*;
- public class Decode{
- public static void main (String[] args) throws IOException{
- String eingabe = args[0];
- String ausgabe = args[1];
- int key = 0;
- boolean makesSense = false;
- if (ausgabe.endsWith(".txt") == false)
- throw new BadFormatException();
- try{
- while (makesSense == false && key <= 26 ){
- System.out.println("mache neue file");
- File entschlusselt = new File(ausgabe);
- FileOutputStream fos = new FileOutputStream(ausgabe);
- FileInputStream fis = new FileInputStream(eingabe);
- while (fis.available() > 0){
- fos.write(decodeChar((char)fis.read(), key));
- }
- if (understand(ausgabe) == true){
- makesSense = true;
- } else {
- System.out.println("erhöhe key");
- key = key + 1;
- }
- fis.close();
- fos.close();
- System.out.println("key:" + key);
- }
- if (makesSense == true)
- System.out.println("Schlüssel war: " + key);
- else
- System.out.println("Konnte nicht Entschlüsseln");
- }catch(FileNotFoundException a) {
- System.out.println("Datei nicht gefunden.");
- }catch(ArrayIndexOutOfBoundsException b){
- System.out.println("Falsche Anzahl von Eingaben.");
- }catch(NumberFormatException d){
- System.out.println("Falsche Eingabetypen.");
- }
- }
- private static char decodeChar(char c, int key) {
- key = key * -1;
- int cleanKey = key%26;
- if(cleanKey < 0)
- cleanKey += 26;
- if (c >= 'a' && c<='z') {
- if (c + cleanKey > 'z')
- return (char) (c + cleanKey - 26);
- return (char) (c + cleanKey);
- } else return c;
- }
- private static boolean understand(String ausgabe)throws IOException{
- 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"};
- String s = "";
- int worterAnzahl = 1;
- int checkedWorter = 0;
- char c;
- try{
- FileInputStream fis2 = new FileInputStream(ausgabe);
- while(fis2.available() >0){
- c = (char) fis2.read();
- if (c >= 'a' && c <= 'z' ){
- s = s + c;
- }
- if (c == ' '){
- for( int i = 0; i<haufig.length; i++){
- System.out.println("checke durch das array");
- if (s.equals(haufig[i])){
- System.out.println("habe wort erkannt");
- checkedWorter = checkedWorter + 1;
- break;
- }
- }
- s = "";
- System.out.println("erhoehe wortanzahl");
- worterAnzahl += 1;
- }
- }
- System.out.println("string: " + s);
- fis2.close();
- }catch(FileNotFoundException a) {
- System.out.println("Datei nicht gefunden.");
- }catch(ArrayIndexOutOfBoundsException b){
- System.out.println("Falsche Anzahl von Eingaben.");
- }catch(NumberFormatException d){
- System.out.println("Falsche Eingabetypen.");
- }
- System.out.println("wortzahl: " + worterAnzahl);
- System.out.println("checkedWorter: " + checkedWorter);
- if ( ((checkedWorter*100)/worterAnzahl) >= 29){
- System.out.println("ende understand");
- return true;
- }else{
- System.out.println("ende understand");
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement