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

variable no reconocida

Estas en el tema de variable no reconocida en el foro de Java en Foros del Web. Hola, tengo un ejercicio que resolver (para mañana :() y tengo un problema. Les escribo mi código: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Java: Ver original public class ...
  #1 (permalink)  
Antiguo 20/06/2013, 09:22
 
Fecha de Ingreso: septiembre-2012
Mensajes: 2
Antigüedad: 11 años, 7 meses
Puntos: 0
variable no reconocida

Hola, tengo un ejercicio que resolver (para mañana :() y tengo un problema. Les escribo mi código:

Código Java:
Ver original
  1. public class FindHiddenWords {
  2.  
  3.   public static final char UNKNOWN = '?';
  4.  
  5.   public FindHiddenWords(int targetWidth, int targetHeight,
  6.       String[] wordsToFind) {
  7.       int matrixWidth = targetWidth;
  8.       int matrixHeight = targetHeight;
  9.   }
  10.  
  11. void initializeMatrix() {
  12.    
  13.       char [][] matrix;
  14.          if (matrixWidth == 0 || matrixHeight == 0){
  15.              matrix [1][1] = UNKNOWN;
  16.          }
  17.          else {
  18.          matrix = new char[matrixWidth][matrixHeight];
  19.          for (int i=1; i<matrixHeight){
  20.              for (int j=1; i< matrixWidth){
  21.                  matrix [i][j] = UNKNOWN;
  22.                  j++;}
  23.              }
  24.              i++;
  25.          }
  26.       }
Bueno, la cosa es que el método FindHiddenWords debería recordar los parámetros de ancho y alto de la matrix. Por eso los declaré como int = matrixWidth y int=matrixHeight. Pero después cuando necesito usarlos en el siguiente método (initializeMatrix) no me deja. Eclipse me dice que tanto matrixWidth como matrixHeight no existen.
¿qué estoy haciendo mal? Gracias por su ayuda!
Angi.

Última edición por razpeitia; 20/06/2013 a las 10:38
  #2 (permalink)  
Antiguo 20/06/2013, 10:43
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: variable no reconocida

1. No tienes un método FindHiddenWords, tienes una clase llamada FindHiddenWords.
2. matrixWidth y matrixHeight los estas declarando dentro del constructor, cuando deberías declararlos fuera del constructor.

Código Java:
Ver original
  1. public class FindHiddenWords {
  2.  
  3.   public static final char UNKNOWN = '?';
  4.   int matrixWidth;
  5.   int matrixHeight;
  6.  
  7.   public FindHiddenWords(int targetWidth, int targetHeight,
  8.       String[] wordsToFind) {
  9.       matrixWidth = targetWidth;
  10.       matrixHeight = targetHeight;
  11.   }
  12.  
  13. void initializeMatrix() {
  14.    
  15.       char [][] matrix;
  16.          if (matrixWidth == 0 || matrixHeight == 0){
  17.              matrix [1][1] = UNKNOWN;
  18.          }
  19.          else {
  20.          matrix = new char[matrixWidth][matrixHeight];
  21.          for (int i=1; i<matrixHeight){
  22.              for (int j=1; i< matrixWidth){
  23.                  matrix [i][j] = UNKNOWN;
  24.                  j++;}
  25.              }
  26.              i++;
  27.          }
  28.       }
  #3 (permalink)  
Antiguo 20/06/2013, 13:19
Avatar de cbretana  
Fecha de Ingreso: junio-2013
Ubicación: Pinar del Rio
Mensajes: 55
Antigüedad: 10 años, 10 meses
Puntos: 0
Respuesta: variable no reconocida

public static final char UNKNOWN = '?';

public FindHiddenWords(int targetWidth, int targetHeight,
String[] wordsToFind) {
int matrixWidth = targetWidth;
int matrixHeight = targetHeight;
}
el problema esta en ke declaras tus variables dentro de este metodo, o sea, la visibilidad de ellas se ve reducida solo al trabajo dentro del metodo, lo mejor ke podrias hacer es declararlas afuera, y asignarles targetWidth y targetHeight, esa es la forma correcta, saludos
__________________
.::Una imagen vale mas que mil palabras::.

Etiquetas: string, variable
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 09:43.