Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package entregable.util;
- import java.util.Scanner;
- import java.io.*;
- public class Util {
- private static final String abc = "abcdefghijklmnopqrstuvwxyz";
- public static String cfr(String inp, int n) {
- char cif, sinCif;
- int posiAbc, posiCif;
- String out = "", aux;
- boolean may;
- for (int i = 0; i < inp.length(); i++) {
- may = false;
- sinCif = inp.charAt(i);
- if (Character.isUpperCase(sinCif)) {
- sinCif = Character.toLowerCase(sinCif);
- may = true;
- }
- posiAbc = abc.indexOf(sinCif);
- if (posiAbc == -1) {
- cif = sinCif;
- } else {
- posiCif = posiAbc + n;
- if (posiCif > 25) {
- posiCif = posiCif % 26;
- } else if (posiCif < 0) {
- posiCif = posiCif + 26;
- }
- cif = abc.charAt(posiCif);
- }
- if (may == true) {
- aux = String.valueOf(cif).toUpperCase();
- } else {
- aux = String.valueOf(cif);
- }
- out = out + aux;
- }
- return out;
- }
- public static int sacarDesplazamiento(String nom) {
- File fi;
- Scanner scFile;
- int n, max = -1, posi = 0;
- int[] ia = new int[26];
- String linea;
- char l;
- fi = new File(nom);
- try {
- scFile = new Scanner(fi);
- while (scFile.hasNextLine()) {
- linea = scFile.nextLine().toLowerCase();
- for (int i = 0; i < linea.length(); i++) {
- l = linea.charAt(i);
- n = abc.indexOf(l);
- if (n != -1) {
- ia[n]++;
- }
- }
- }
- for (int i = 0; i < ia.length; i++) {
- if (ia[i] > max) {
- max = ia[i];
- posi = i;
- }
- }
- } catch (FileNotFoundException ex) {
- System.out.println("No existe el fichero.");
- }
- return 4 - posi;
- }
- public static void common(String nom, int desp, File fo) throws Exception {
- if (desp > 25 || desp < -25) {
- throw new Exception();
- }
- File fi;
- FileWriter fw = null;
- Scanner scFile = null;
- String linea;
- fi = new File(nom);
- if (!fi.exists()) {
- System.out.println("El fichero no existe.");
- } else {
- try {
- scFile = new Scanner(fi, "UTF-8");
- fw = new FileWriter(fo);
- while (scFile.hasNextLine()) {
- linea = scFile.nextLine();
- linea = cfr(linea, desp);
- fw.write(linea + "\n");
- }
- } catch (FileNotFoundException ex) {
- System.out.println("Archivo no encontrado.");
- } catch (IOException ex) {
- System.out.println("Error en el fichero de escritura.");
- } finally {
- if (scFile != null) {
- scFile.close();
- }
- if (fw != null) {
- try {
- fw.close();
- } catch (IOException e) {
- System.out.println("Error al cerrar el fichero de escritura.");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement