Foro de elhacker.net

Programación => Java => Mensaje iniciado por: klaine en 12 Abril 2011, 22:59 pm



Título: [Arreglos] "Array required but int found" [RESUELTO]
Publicado por: klaine en 12 Abril 2011, 22:59 pm
Hola, tengo un problema con lo siguiente:

Instancié tres arreglos de la siguiente forma:

int[] a = new int[9];
int[] b = new int[9];
int[] c = new int[9];

creé tres índices para los arreglos de la siguiente forma:

int ia = 0, ib = 0, ic = 0;

El problema es que si en c[ic] guardo un valor el compilador me suelta "Array required but int found "  :-\, no se que estoy haciendo mal, les dejo el código, de antemano gracias por sus respuestas  :D

Saludos  ;)

Código
  1. import javax.swing.*;
  2.  
  3. public class clase{
  4.  
  5. boolean ctp;
  6. int[][] vec = new int[3][3];
  7. int[] a = new int[9];
  8. int[] b = new int[9];
  9. int[] c = new int[9];
  10. int ia = 0, ib = 0, ic = 0;
  11.  
  12. public clase(int ctrl){
  13.  
  14. switch(ctrl){
  15.  
  16. case 1:
  17.  
  18. sub_principal();
  19.  
  20. break;
  21. case 2:
  22.  
  23. sub_numeros();
  24.  
  25. break;
  26. case 3:
  27.  
  28. sub_arreglos();
  29.  
  30.  break;
  31. case 4:
  32.  
  33. System.exit(0);
  34.  
  35.  break;
  36.  
  37. }
  38.  
  39. }
  40.  
  41. public static void Main(String[] args){
  42.  
  43. }
  44.  
  45. void sub_principal(){
  46.  
  47. ctp=true;
  48.  
  49. while(ctp){
  50.  
  51. int m = Integer.parseInt(JOptionPane.showInputDialog(null, "1- Ingresar una frase para contar sus caracteres \n\n 2- Ingresar un Nombre para mostrar en minúsculas o mayúsculas \n\n 3- Volver \n\n 4- Salir"));
  52.  
  53. switch(m){
  54.  
  55. case 1:
  56.  
  57. String frase = JOptionPane.showInputDialog(null, "Ingrese una frase");
  58.  
  59. JOptionPane.showMessageDialog(null, "La frase tiene " + frase.length() + " caracteres");
  60.  
  61.  break;
  62. case 2:
  63.  
  64. String nombre = JOptionPane.showInputDialog(null, "Ingrese un nombre");
  65.  
  66. JOptionPane.showMessageDialog(null, nombre.toUpperCase());
  67.  
  68.  break;
  69. case 3:
  70.  
  71. ctp=false;
  72.  
  73.  break;
  74. case 4:
  75.  
  76. new clase(4);
  77.  
  78.  break;
  79.  
  80. }
  81.  
  82. new clase(1);
  83.  
  84. }
  85.  
  86. }
  87.  
  88. void sub_numeros(){
  89.  
  90. ctp=true;
  91.  
  92. while(ctp){
  93.  
  94. int s = Integer.parseInt(JOptionPane.showInputDialog(null, "1- Ver si el numero es primo \n\n 2- Ver si el numero es perfecto \n\n 3- Calcular MCM entre dos numeros \n\n 4- Volver 5- Salir"));
  95.  
  96. switch(s){
  97.  
  98. case 1:
  99.  
  100. int op = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el numero"));
  101.  
  102. if (esPrimo(op)){
  103.  
  104. JOptionPane.showMessageDialog(null, s + "Es primo");
  105.  
  106. }else{
  107.  
  108. JOptionPane.showMessageDialog(null, s + "No es primo");
  109.  
  110. }
  111.  
  112.   break;
  113. case 2:
  114.  
  115. int op2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese el numero"));
  116.  
  117. if (esPerfecto(s)){
  118.  
  119. JOptionPane.showMessageDialog(null, s + "Es perfecto");
  120.  
  121. }else{
  122.  
  123. JOptionPane.showMessageDialog(null, s + "No es perfecto");
  124.  
  125. }
  126.  
  127.   break;
  128. case 3:
  129.  
  130. JOptionPane.showMessageDialog(null, "Aun no implementada XD");
  131.  
  132.   break;
  133. case 4:
  134.  
  135. ctp=false;
  136.  
  137.   break;
  138. case 5:
  139.  
  140. new clase(4);
  141.  
  142.   break;
  143.  
  144. }
  145.  
  146. new clase(1);
  147.  
  148. }
  149.  
  150. }
  151.  
  152. void sub_arreglos(){
  153.  
  154. ctp=true;
  155.  
  156. while(ctp){
  157.  
  158. int in = Integer.parseInt(JOptionPane.showInputDialog(null, "1- Ingresar numeros para la matriz \n\n 2- Volver \n\n 3-Salir"));
  159.  
  160. switch (in){
  161.  
  162. case 1:
  163.  
  164. int c=1;
  165.  
  166. for (int x=0;x<3;x++){
  167.  
  168.  for (int y=0;y<3;y++){
  169.  
  170.  vec[x][y] = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese numero " + c));
  171.  c++;
  172.  
  173.  if(vec[x][y]%2==0){
  174.  
  175.  a[ia] = vec[x][y];
  176.  ia++;
  177.  
  178.  }else if (vec[x][y]%2!=0){
  179.  
  180.  b[ib] = vec[x][y];
  181.  ib++;
  182.  
  183.  }
  184.  
  185.  if (vec[x][y]<0){
  186.  
  187.  c[ic] = vec[x][y];
  188.  ic++;
  189.  
  190.  }
  191.  
  192.  }
  193.  
  194. }
  195.  
  196. String pares="";
  197.  
  198. for (int valor:a){
  199.  
  200. pares += "[" + Integer.toString(valor) + "]";
  201.  
  202. }
  203.  
  204. JOptionPane.showMessageDialog(null, "Los numeros pares son \n\n" + pares);
  205.  
  206. String impares="";
  207.  
  208. for (int valor:a){
  209.  
  210. impares += "[" + Integer.toString(valor) + "]";
  211.  
  212. }
  213.  
  214. JOptionPane.showMessageDialog(null, "Los numeros impares son \n\n" + impares);
  215.  
  216. String negativos="";
  217.  
  218. for (int valor:a){
  219.  
  220. negativos += "[" + Integer.toString(valor) + "]";
  221.  
  222. }
  223.  
  224. JOptionPane.showMessageDialog(null, "Los numeros negativos son \n\n" + negativos);
  225.  
  226. break;
  227.  
  228. case 2:
  229.  
  230. ctp = false;
  231.  
  232.    break;
  233. case 3:
  234.  
  235. new clase(4);
  236.  
  237.    break;
  238.  
  239.  }
  240.  
  241.   }
  242.  
  243.  new clase(1);
  244.  
  245. }
  246.  
  247. boolean esPrimo(int p){
  248.  
  249. int c;
  250.  
  251. for (int x=p;x>0;x--){
  252.  
  253. if (p%x==0){
  254.  
  255. c++;
  256.  
  257. }
  258.  
  259. }
  260.  
  261. if (c==2){
  262.  
  263. return true;
  264.  
  265. }else{
  266.  
  267. return false;
  268.  
  269. }
  270.  
  271. }
  272.  
  273. boolean esPerfecto(int p){
  274.  
  275. int res=0;
  276.  
  277.    for(int y=1;y<=p/2;y++){
  278.  
  279.        if (p%y==0){
  280.  
  281.            res = res + y;
  282.  
  283.        }
  284.  
  285.    }
  286.  
  287.    if (res == p) {
  288.  
  289.            return true;
  290.  
  291.        }else{
  292.  
  293.            return false;
  294.  
  295.        }
  296.  
  297.    }
  298.  
  299. }
  300.  


Título: Re: [Arreglos] "Array required but int found"
Publicado por: NetJava en 13 Abril 2011, 02:46 am
Buenas, ya se que lo has especificado, pero el resto de arrays no dan problemas¿?


Título: Re: [Arreglos] "Array required but int found"
Publicado por: klaine en 13 Abril 2011, 22:10 pm
Nop, el resto no da problemas, pero ya me dijeron en otro foro, loque pasa es que dentro del método inicié una variable local de tipo int llamada "c", entonces se blokea el scope del array global del mismo nombre, XD, bueno, gracias de todos modos.  :D

Saludos  ;)