Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6.  
  7. /*
  8. * To change this license header, choose License Headers in Project Properties.
  9. * To change this template file, choose Tools | Templates
  10. * and open the template in the editor.
  11. */
  12. /**
  13. *
  14. * @author anton
  15. */
  16. public class SinClave {
  17.  
  18. public static void main(String[] args) {
  19.  
  20. HashMap<String, Integer> numLetras = new HashMap<>();
  21. String nombreFicheroEntrada = (args[0]).concat(".cfr");
  22.  
  23. String nombreFicheroSalida = "sinclave.txt";
  24. getClave(nombreFicheroEntrada, numLetras);
  25. int clave = getClave(nombreFicheroEntrada, numLetras);
  26. System.out.println("Letra ganadora: " + letraGanadora(numLetras));
  27. System.out.println("CLAVE " + clave);
  28.  
  29. String linea = "";
  30.  
  31. }
  32.  
  33. public static int getClave(String nombreFicheroEntrada, HashMap<String, Integer> numLetras) {
  34. int solucion;
  35. File f;
  36. String linea = "";
  37. String letra = "";
  38. f = new File(nombreFicheroEntrada);
  39.  
  40. Scanner scFile = null;
  41. try {
  42. scFile = new Scanner(f, "UTF-8");
  43. while (scFile.hasNextLine()) {
  44. linea = scFile.nextLine();
  45. for (int i = 0; i < linea.length(); i++) {
  46. letra = Character.toString(linea.charAt(i)) ;
  47. cuentaLetras(numLetras, letra);
  48.  
  49. }
  50. }
  51. // System.out.println("Linea: \n" + linea);
  52. } catch (Exception e) {
  53. System.out.println("Se ha producido un error: " + e.toString());
  54. } finally {
  55. if (scFile != null) {
  56. scFile.close();
  57. }
  58. }
  59.  
  60. String casiClave = (letraGanadora(numLetras));
  61. // cuentaLetras(numLetras, letra);
  62. char cc = casiClave.charAt(0);
  63. solucion = cc - 'e';
  64.  
  65. return solucion;
  66. }
  67.  
  68. public static void cuentaLetras(HashMap<String, Integer> numLetras, String letra) {
  69. //Mete las letras en el HashMap
  70. if (!numLetras.containsKey(letra)) {
  71. numLetras.put(letra, 1);
  72. } else {
  73. numLetras.put(letra, numLetras.get(letra) + 1);
  74. }
  75. }
  76.  
  77. public static String letraGanadora(HashMap<String, Integer> numLetras) {
  78. //Te saca la letra mรกs repetida
  79. String ganadora = "f";
  80. int vecesLetra = 0;
  81. for (String i : numLetras.keySet()) {
  82. if (numLetras.get(i) > vecesLetra) {
  83. ganadora = i;
  84. vecesLetra = numLetras.get(i);
  85. } else if (numLetras.get(i) == vecesLetra) {
  86. ganadora = "EMPATE";
  87. }
  88. }
  89. System.out.println("___ " + ganadora + " ____________");
  90. return ganadora;
  91.  
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement