Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.HashMap;
- import java.util.Scanner;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author anton
- */
- public class SinClave {
- public static void main(String[] args) {
- HashMap<String, Integer> numLetras = new HashMap<>();
- String nombreFicheroEntrada = (args[0]).concat(".cfr");
- String nombreFicheroSalida = nombreFicheroEntrada.concat(".uncfr");
- String texto = "";
- File f;
- f = new File(nombreFicheroEntrada);
- Scanner scFile = null;
- File fw = new File(nombreFicheroSalida);
- int clave = leoFicheroSacoClave(f, scFile, texto, numLetras) * -1;
- Utils.leoYEscribo(f, clave, nombreFicheroSalida);
- }
- public static boolean esLetra(String letra) {
- boolean resultado = false;
- if (letra.charAt(0) >= 'a' && letra.charAt(0) <= 'z'
- || letra.charAt(0) >= 'A' && letra.charAt(0) <= 'Z') {
- resultado = true;
- } else {
- resultado = false;
- }
- return resultado;
- }
- public static int getClave(HashMap<String, Integer> numLetras) {
- int clave = 0;
- int vigente = 0;
- int cantidadActual = 0;
- String letraMas = "";
- for (String c : numLetras.keySet()) {
- cantidadActual = numLetras.get(c);
- if (cantidadActual > vigente) {
- vigente = cantidadActual;
- letraMas = c;
- }
- // cifrado.append((char) (texto.charAt(i) + clave - 26))
- }
- clave = ((char) (letraMas.toLowerCase().charAt(0)) - 101);
- return clave;
- }
- public static void cuentaLetras(HashMap<String, Integer> numLetras, String linea) {
- String letra = "";
- for (int i = 0; i < linea.length(); i++) {
- letra = Character.toString(linea.toLowerCase().charAt(i));
- if (esLetra(letra) == true) {
- if (!numLetras.containsKey(letra)) {
- numLetras.put(letra.toLowerCase(), 1);
- } else {
- numLetras.put(letra.toLowerCase(), numLetras.get(letra) + 1);
- }
- }
- }
- }
- public static int leoFicheroSacoClave(File f, Scanner scFile, String entrada,
- HashMap<String, Integer> numLetras) {
- int superClave = 0;
- try {
- if (f.exists()) {
- {
- scFile = new Scanner(f);
- while (scFile.hasNextLine()) {
- entrada = scFile.nextLine();
- cuentaLetras(numLetras, entrada);
- }
- superClave = getClave(numLetras);
- }
- }
- } catch (Exception e) {
- System.out.println(e.toString());
- } finally {
- if (scFile != null) {
- scFile.close();
- }
- }
- return superClave;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement