Advertisement
Ferarias30

MDPYPC-TP02-5

Apr 8th, 2020
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 KB | None | 0 0
  1. package ar.edu.unju.Punto05;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.util.LinkedList;
  9.  
  10.  
  11. public class leerTexto {
  12.     int cont = 0;
  13.     private static LinkedList<String> arregloPar = new LinkedList<String>();
  14.     private static LinkedList<String> arregloImpar = new LinkedList<String>();
  15.     public String read_text(String texto) {
  16.  
  17.         try {
  18.             BufferedReader bf = new BufferedReader(new FileReader("C:\\Users\\Arias\\Documents\\FER\\txt\\TP02Frase.txt"));
  19.             String temp = "";
  20.             String bfRead;
  21.            
  22.             while((bfRead = bf.readLine()) != null) {
  23.                 temp = temp + bfRead +" ";
  24.                 if (cont % 2 == 0) {
  25.                     arregloPar.add(bfRead);
  26.                 }else {
  27.                     arregloImpar.add(bfRead);
  28.                 }
  29.                 cont ++;
  30.             }
  31.             texto = temp;
  32.         } catch (Exception e) {
  33.             // TODO: handle exception
  34.             System.err.println("No se encontro archivo.");
  35.         }
  36.         return texto;
  37.     }
  38.    
  39.     public void intercambiar_lineas() {
  40.         try {
  41.             File archivo = new File("C:\\Users\\Arias\\Documents\\FER\\txt\\TP02FraseCambiada.txt");
  42.             BufferedWriter bw;
  43.             bw = new BufferedWriter(new FileWriter(archivo));
  44.             do {
  45.                 if(!arregloImpar.isEmpty()) {
  46.                     bw.write(arregloImpar.pop());
  47.                     bw.newLine();
  48.                 }
  49.                 if(!arregloPar.isEmpty()) {
  50.                     bw.write(arregloPar.pop());
  51.                     bw.newLine();
  52.                 }
  53.             }while(!arregloPar.isEmpty()||!arregloImpar.isEmpty());
  54.             bw.close();
  55.         } catch (Exception e) {
  56.             // TODO: handle exception
  57.             System.err.println("No se puedo escribir el archivo.");
  58.         }
  59.     }
  60.    
  61.     public void cont_lineas() {
  62.         System.out.println("La cantidad de lineas es: "+cont);
  63.     }
  64. }
  65. ---------------------------------------------------------------------------------------------------------
  66. package ar.edu.unju.Punto05;
  67.  
  68. import java.util.StringTokenizer;
  69.  
  70. public class Archivos {
  71.     public static void main(String[] args) {
  72.         leerTexto a = new leerTexto();
  73.         String texto = "";
  74.         texto = a.read_text(texto);
  75.         System.out.println(texto);
  76.        
  77.         contar_vocales(texto);
  78.         espacios_blancos(texto);
  79.         cant_palabras(texto);
  80.         cant_mayusculas(texto);
  81.         a.cont_lineas();
  82.         a.intercambiar_lineas();
  83.     }
  84.    
  85.     public static void cant_palabras(String texto) {
  86.         StringTokenizer st = new StringTokenizer(texto);
  87.         System.out.println("La cantidad de palabras es: "+st.countTokens());
  88.     }
  89.    
  90.     public static void cant_mayusculas(String texto) {
  91.         int cont = 0;
  92.         for(int i=0; i<texto.length(); i++) {
  93.             char temp = texto.charAt(i);
  94.             char mayus = Character.toUpperCase(temp);
  95.             if((mayus == temp) && (mayus != ' ')){
  96.                 cont++;
  97.             }
  98.         }
  99.         System.out.println("La cantidad de mayusuclas es: " + cont);
  100.     }
  101.    
  102.     public static void espacios_blancos(String texto) {
  103.         int cont_espacios = 0;
  104.         for(int i=0; i<texto.length(); i++) {
  105.             char temp = texto.charAt(i);
  106.             if(temp == ' '){
  107.                 cont_espacios++;
  108.             }
  109.         }
  110.         System.out.println("La cantidad de espacios en blanco es: "+(cont_espacios-1));
  111.     }
  112.    
  113.     public static void contar_vocales(String texto) {
  114.        
  115.         int contVocales = 0;
  116.        
  117.         for(int i=0; i<texto.length(); i++) {
  118.            
  119.             char temp = texto.charAt(i);
  120.            
  121.             if(temp == 'a' || temp =='A') {
  122.                 contVocales ++;
  123.             }else if(temp == 'e' || temp =='E') {
  124.                 contVocales ++;
  125.             }else if(temp == 'i' || temp =='I') {
  126.                 contVocales ++;
  127.             }else if(temp == 'o' || temp =='O') {
  128.                 contVocales ++;
  129.             }else if(temp == 'u' || temp =='U') {
  130.                 contVocales ++;
  131.             }
  132.         }
  133.        
  134.         System.out.println("La cantidad de vocales que tiene el texto es: "+contVocales);
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement