Advertisement
Carlos98

Descifrar Sin Rango

Mar 31st, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class DescifrarSinRango {
  5.  
  6.     static String abecedario = Utils.alfabeto;
  7.     static int[] contador;
  8.  
  9.     public static int generarNumero(String ruta) {
  10.  
  11.         String texto;
  12.  
  13.         int n, max = 0, pos = 0;
  14.         char letra;
  15.         File archivo = null;
  16.         Scanner scFile = null;
  17.  
  18.         try {
  19.  
  20.             archivo = new File(ruta);
  21.  
  22.             scFile = new Scanner(archivo);
  23.  
  24.             while (scFile.hasNext()) {
  25.                 texto = scFile.nextLine().toLowerCase();
  26.  
  27.                 for (int i = 0; i < texto.length(); i++) {
  28.                     letra = texto.charAt(i);
  29.                     n = abecedario.indexOf(letra);
  30.                     if (n != -1) {
  31.                         contador[n]++;
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             max = contador[0];
  37.             pos = 0;
  38.  
  39.             for (int i = 1; i < contador.length; i++) {
  40.                 if (contador[i] > max) {
  41.                     max = contador[i];
  42.                     pos = i;
  43.                 }
  44.             }
  45.  
  46.         } catch (Exception e) {
  47.             System.out.println(e.getMessage());
  48.             e.printStackTrace();
  49.         } finally {
  50.             try {
  51.                 scFile.close();
  52.             } catch (Exception e) {
  53.                 System.out.println(e.getMessage());
  54.                 e.printStackTrace();
  55.             }
  56.         }
  57.  
  58.         return pos - abecedario.indexOf("e");
  59.     }
  60.  
  61.     public static void main(String[] args) {
  62.  
  63.         String ruta;
  64.         boolean hechoCorrectamente = false;
  65.  
  66.         contador = new int[abecedario.length()];
  67.  
  68.         int numeroDesplazamiento = generarNumero(args[0]);
  69.  
  70.         hechoCorrectamente = Utils.escribirTexto(args[0], args[0] + ".uncfr", 26 - numeroDesplazamiento);
  71.  
  72.         if (hechoCorrectamente) {
  73.             System.out.println("Se ha generado correctamente");
  74.         } else {
  75.             System.out.println("No se ha podido generar");
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement