Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- 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 = "sinclave.txt";
- getClave(nombreFicheroEntrada, numLetras);
- int clave = getClave(nombreFicheroEntrada, numLetras);
- System.out.println("Letra ganadora: " + letraGanadora(numLetras));
- System.out.println("CLAVE " + clave);
- String linea = "";
- }
- public static int getClave(String nombreFicheroEntrada, HashMap<String, Integer> numLetras) {
- int solucion;
- File f;
- String linea = "";
- String letra = "";
- f = new File(nombreFicheroEntrada);
- Scanner scFile = null;
- try {
- scFile = new Scanner(f, "UTF-8");
- while (scFile.hasNextLine()) {
- linea = scFile.nextLine();
- for (int i = 0; i < linea.length(); i++) {
- letra = Character.toString(linea.charAt(i)) ;
- cuentaLetras(numLetras, letra);
- }
- }
- // System.out.println("Linea: \n" + linea);
- } catch (Exception e) {
- System.out.println("Se ha producido un error: " + e.toString());
- } finally {
- if (scFile != null) {
- scFile.close();
- }
- }
- String casiClave = (letraGanadora(numLetras));
- // cuentaLetras(numLetras, letra);
- char cc = casiClave.charAt(0);
- solucion = cc - 'e';
- return solucion;
- }
- public static void cuentaLetras(HashMap<String, Integer> numLetras, String letra) {
- //Mete las letras en el HashMap
- if (!numLetras.containsKey(letra)) {
- numLetras.put(letra, 1);
- } else {
- numLetras.put(letra, numLetras.get(letra) + 1);
- }
- }
- public static String letraGanadora(HashMap<String, Integer> numLetras) {
- //Te saca la letra mรกs repetida
- String ganadora = "f";
- int vecesLetra = 0;
- for (String i : numLetras.keySet()) {
- if (numLetras.get(i) > vecesLetra) {
- ganadora = i;
- vecesLetra = numLetras.get(i);
- } else if (numLetras.get(i) == vecesLetra) {
- ganadora = "EMPATE";
- }
- }
- System.out.println("___ " + ganadora + " ____________");
- return ganadora;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement