Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/05/2014, 01:23
SilverDante
 
Fecha de Ingreso: diciembre-2013
Mensajes: 36
Antigüedad: 10 años, 5 meses
Puntos: 1
Respuesta: Histograma con asteriscos, contar palabras

Hecho de otro modo, pero también el resultado no es correcto.

Código PHP:
Ver original
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package pruebas;
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. /**
  11.  *
  12.  * @author Joan
  13.  */
  14. public class Pruebas {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args) {
  20.         //ArrayList<Integer> histo = new ArrayList<Integer>();
  21.         int i=0;
  22.         //int[] histo = new int[i];
  23.         //int[] hist2 = new int[15];
  24.        
  25.         { File archivo = null;
  26.         try {
  27.         archivo = new File("c:\\ficheros\\fichero.txt");//"archivo.txt" es el archivo que va a leer
  28.         String linea, f1=("c:\\ficheros\\fichero.txt");
  29.         FileReader fr = new FileReader (archivo);
  30.         BufferedReader br = new BufferedReader(fr);
  31.         Scanner sc = new Scanner(new File(f1));
  32.        
  33.         int j,a=0;
  34.        
  35.         estadisticas(sc);
  36.         sc.close();
  37.        
  38.         fr.close();
  39.         }
  40.         catch(IOException a){
  41.         System.out.println(a);
  42.         }
  43.        
  44.         }
  45.        
  46.     }
  47.    
  48.     static void estadisticas(Scanner s) {
  49.        // ArrayList<Integer> histo = new ArrayList<Integer>();
  50.         int[] hist2 = new int[15];
  51.         int nPalabras = 0, nChars = 0, i = 0, u1=0, c=0;
  52.         String p=null;
  53.        
  54.         while (s.hasNext()){
  55.            
  56.             p = s.next();
  57.             nPalabras++;
  58.             nChars+=p.length();
  59.            
  60.             for(i=0;i<16;i++){
  61.                 if(p.length()==i){
  62.                 u1++;
  63.                 hist2[i]=u1;
  64.                 }
  65.                
  66.                 //hist2[i]=u1;
  67.                 //histo.add(u1);
  68.             }
  69.            
  70.         }
  71.        
  72.         System.out.println("");
  73.     System.out.println("Numero total de palabras: " + nPalabras);
  74.         System.out.println("");
  75.        
  76.     for(i=0;i<16;i++){
  77.         System.out.println("Palabras de logitud "+i+": "+hist2[i]);
  78.     }
  79.  
  80. }
  81.    
  82. }

Otro intento erróneo. No se como hacer que cuente bien. :(

Código PHP:
Ver original
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package pruebas;
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. /**
  11.  *
  12.  * @author Joan
  13.  */
  14. public class Pruebas {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args) {
  20.         //ArrayList<Integer> histo = new ArrayList<Integer>();
  21.         int i=0;
  22.         //int[] histo = new int[i];
  23.         //int[] hist2 = new int[15];
  24.        
  25.         { File archivo = null;
  26.         try {
  27.         archivo = new File("c:\\ficheros\\fichero.txt");//"archivo.txt" es el archivo que va a leer
  28.         String linea, f1=("c:\\ficheros\\fichero.txt");
  29.         //FileReader fr = new FileReader (archivo);
  30.         //BufferedReader br = new BufferedReader(fr);
  31.         Scanner sc = new Scanner(new File(f1));
  32.        
  33.         int j,a=0;
  34.        
  35.         estadisticas(sc);
  36.         sc.close();
  37.        
  38.         //fr.close();
  39.         }
  40.         catch(IOException a){
  41.         System.out.println(a);
  42.         }
  43.        
  44.         }
  45.        
  46.     }
  47.    
  48.     static void estadisticas(Scanner s) {
  49.        // ArrayList<Integer> histo = new ArrayList<Integer>();
  50.         int[] hist2 = new int[16];
  51.         int nPalabras = 0, i = 0, u1=0;
  52.         String p=null;
  53.        
  54.         while (s.hasNext()){
  55.            
  56.             p = s.next();
  57.             nPalabras++;
  58.            
  59.             for(i=1;i<=16;i++){
  60.                
  61.                 if(p.length()==i){
  62.                 u1++;
  63.                 //hist2[i]=u1;
  64.                 }
  65.                 while(p.length()==i||i!=16){
  66.                     hist2[i]=u1;
  67.                    
  68.                 }
  69.                
  70.             }
  71.            
  72.         }
  73.        
  74.        
  75.         System.out.println("");
  76.     System.out.println("Numero total de palabras: " + nPalabras);
  77.         System.out.println("");
  78.        
  79.     for(i=1;i<16;i++){
  80.         System.out.println("Palabras de logitud "+i+": "+hist2[i]);
  81.     }

Última edición por SilverDante; 20/05/2014 a las 02:40