Foros del Web » Programación para mayores de 30 ;) » Java »

Histograma con asteriscos, contar palabras

Estas en el tema de Histograma con asteriscos, contar palabras en el foro de Java en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 20/05/2014, 01:08
 
Fecha de Ingreso: diciembre-2013
Mensajes: 36
Antigüedad: 10 años, 4 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. }
  #2 (permalink)  
Antiguo 20/05/2014, 01:23
 
Fecha de Ingreso: diciembre-2013
Mensajes: 36
Antigüedad: 10 años, 4 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

Etiquetas: arrays, palabras, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:02.