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

Deseo que un código en JAVA, cuente la cantidad de palabras que son de 1 a 15 caracteres de longitud y luego lo represente en un diagrama horizontal, mediante "*", tantas veces como aparece la palabra.

Mi código es este, no esta terminado y el resultado que da no es correcto, pero no se como resolverlo.

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 nPalabras = 0, nChars = 0, i = 0, u1=0, c=0;
  51.         String p=null;
  52.        
  53.         while (s.hasNext()){
  54.            
  55.             p = s.next();
  56.             nPalabras++;
  57.             nChars+=p.length();
  58.            
  59.             for(i=0;i<16;i++){
  60.                 if(p.length()==i){
  61.                 u1++;
  62.                 }
  63.                
  64.                 histo.add(u1);
  65.             }
  66.            
  67.         }
  68.        
  69.         System.out.println("");
  70.     System.out.println("Numero total de palabras: " + nPalabras);
  71.         System.out.println("");
  72.        
  73.     Iterator t = histo.iterator();
  74.    
  75.     while(t.hasNext()){
  76.         c++;
  77.         System.out.println("Palabras de "+c+": "+t.next());
  78.     }
  79.  
  80. }
  81.    
  82. }