2010-03-31 10 views
11

Mi programa se supone que cuenta la ocurrencia de cada carácter en un archivo ignorando mayúsculas y minúsculas. El método que escribí es:¿Por qué obtengo basura cuando imprimo un int []?

public int[] getCharTimes(File textFile) throws FileNotFoundException { 

    Scanner inFile = new Scanner(textFile); 

    int[] lower = new int[26]; 
    char current; 
    int other = 0; 

    while(inFile.hasNext()){ 
    String line = inFile.nextLine(); 
    String line2 = line.toLowerCase(); 
    for (int ch = 0; ch < line2.length(); ch++) { 
     current = line2.charAt(ch); 
     if(current >= 'a' && current <= 'z') 
      lower[current-'a']++; 
     else 
      other++; 
    } 
    } 

    return lower; 
} 

Y se imprime usando:

for(int letter = 0; letter < 26; letter++) { 
      System.out.print((char) (letter + 'a')); 
     System.out.println(": " + ts.getCharTimes(file)); 
      } 

Dónde TS es un objeto TextStatistic creado anteriormente en mi método principal. Sin embargo, cuando ejecuto mi programa, en lugar de imprimir el número de con qué frecuencia se produce el carácter que imprime:

a: [[email protected] 
b: [[email protected] 
c: [[email protected] 
d: [[email protected] 
e: [[email protected] 
f: [[email protected] 

Y no sé lo que estoy haciendo mal.

Respuesta

3

ts.getCharTimes (archivo) devuelve array int.

ts.getCharTimes de impresión (archivo) [carta]

+0

Gracias! ¡Trabajado como un encanto! – Kat

+0

y lo que smink respondió – Nishu

9

Compruebe la firma de su método; está devolviendo una matriz de enteros.

ts.getCharTimes (archivo) devuelve int matriz. Así que para el uso de impresión:

ts.getCharTimes(file)[letter] 

También está ejecutando el método 26 veces, lo cual es probable que sea errónea. Desde el contexto de llamada (parámetros y tal) no se ve afectada por las iteraciones del bucle considere cambiar el código para:

int[] letterCount = ts.getCharTimes(file); 
for(int letter = 0; letter < 26; letter++) { 
    System.out.print((char) (letter + 'a')); 
    System.out.println(": " + letterCount[letter]); 
} 
2

No es basura; es una característica!

public static void main(String[] args) { 
    System.out.println(args); 
    System.out.println("long: " + new long[0]); 
    System.out.println("int:  " + new int[0]); 
    System.out.println("short: " + new short[0]); 
    System.out.println("byte: " + new byte[0]); 
    System.out.println("float: " + new float[0]); 
    System.out.println("double: " + new double[0]); 
    System.out.println("boolean: " + new boolean[0]); 
    System.out.println("char: " + new char[0]); 
} 
 
[Ljava.lang.String;@70922804 
long: [[email protected] 
int:  [[email protected] 
short: [[email protected] 
byte: [[email protected] 
float: [[email protected] 
double: [[email protected] 
boolean: [[email protected] 
char: [[email protected] 

"Las clases de matrices tienen nombres extraños que no son identificadores válidos;" - The Java Virtual Machine Specification.

Adición: Vea también toString().

Cuestiones relacionadas