Advertisement
Guest User

utils

a guest
Apr 1st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. package entregable.util;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5.  
  6. public class Util {
  7.  
  8. private static final String abc = "abcdefghijklmnopqrstuvwxyz";
  9.  
  10. public static String cfr(String inp, int n) {
  11. char cif, sinCif;
  12. int posiAbc, posiCif;
  13. String out = "", aux;
  14. boolean may;
  15.  
  16. for (int i = 0; i < inp.length(); i++) {
  17. may = false;
  18. sinCif = inp.charAt(i);
  19. if (Character.isUpperCase(sinCif)) {
  20. sinCif = Character.toLowerCase(sinCif);
  21. may = true;
  22. }
  23. posiAbc = abc.indexOf(sinCif);
  24. if (posiAbc == -1) {
  25. cif = sinCif;
  26. } else {
  27. posiCif = posiAbc + n;
  28. if (posiCif > 25) {
  29. posiCif = posiCif % 26;
  30. } else if (posiCif < 0) {
  31. posiCif = posiCif + 26;
  32. }
  33. cif = abc.charAt(posiCif);
  34. }
  35. if (may == true) {
  36. aux = String.valueOf(cif).toUpperCase();
  37. } else {
  38. aux = String.valueOf(cif);
  39. }
  40. out = out + aux;
  41. }
  42.  
  43. return out;
  44. }
  45.  
  46. public static int sacarDesplazamiento(String nom) {
  47. File fi;
  48. Scanner scFile;
  49. int n, max = -1, posi = 0;
  50. int[] ia = new int[26];
  51. String linea;
  52. char l;
  53.  
  54. fi = new File(nom);
  55. try {
  56. scFile = new Scanner(fi);
  57. while (scFile.hasNextLine()) {
  58. linea = scFile.nextLine().toLowerCase();
  59. for (int i = 0; i < linea.length(); i++) {
  60. l = linea.charAt(i);
  61. n = abc.indexOf(l);
  62. if (n != -1) {
  63. ia[n]++;
  64. }
  65.  
  66. }
  67.  
  68. }
  69.  
  70. for (int i = 0; i < ia.length; i++) {
  71. if (ia[i] > max) {
  72. max = ia[i];
  73. posi = i;
  74. }
  75. }
  76.  
  77. } catch (FileNotFoundException ex) {
  78. System.out.println("No existe el fichero.");
  79. }
  80.  
  81. return 4 - posi;
  82. }
  83.  
  84. public static void common(String nom, int desp, File fo) throws Exception {
  85. if (desp > 25 || desp < -25) {
  86. throw new Exception();
  87. }
  88. File fi;
  89. FileWriter fw = null;
  90. Scanner scFile = null;
  91. String linea;
  92.  
  93. fi = new File(nom);
  94. if (!fi.exists()) {
  95. System.out.println("El fichero no existe.");
  96. } else {
  97.  
  98. try {
  99. scFile = new Scanner(fi, "UTF-8");
  100. fw = new FileWriter(fo);
  101.  
  102. while (scFile.hasNextLine()) {
  103. linea = scFile.nextLine();
  104. linea = cfr(linea, desp);
  105. fw.write(linea + "\n");
  106. }
  107.  
  108. } catch (FileNotFoundException ex) {
  109. System.out.println("Archivo no encontrado.");
  110. } catch (IOException ex) {
  111. System.out.println("Error en el fichero de escritura.");
  112. } finally {
  113. if (scFile != null) {
  114. scFile.close();
  115. }
  116. if (fw != null) {
  117. try {
  118. fw.close();
  119. } catch (IOException e) {
  120. System.out.println("Error al cerrar el fichero de escritura.");
  121. }
  122. }
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement