Foro de elhacker.net

Programación => Java => Mensaje iniciado por: javier210186 en 12 Marzo 2013, 05:31 am



Título: pasar un programa de java.swit a java.io
Publicado por: javier210186 en 12 Marzo 2013, 05:31 am
Hola saludos, de antemano agradezco si me pueden guiar un poco, tengo que pasar un código que esta en java.swit a java.io, que reciba datos por el teclado. el código lo llevo así: No da errores de sintaxis, pero quisiera que me mostrara el menu en la consola. el codigo esta un poco largo, pero el problema esta en la linea 125 en adelante, creo, es ahi donde no he podido mostrar el string menu.
Código
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Arreglo
  5. {
  6. int A[]=new int[100];
  7. int aux;
  8. int i,j,n;
  9. public Arreglo(int B[]) //Constructor
  10. {
  11. if(B.length<=100)
  12. n=B.length;
  13. else
  14. n=100;
  15. for(i=0;i<n;i++)
  16. A[i]=B[i];
  17. }
  18. boolean verificarasc() // Verifica Ordenamiento ascendente
  19. {
  20. for(i=0;i<n-1 && A[i]<=A[i+1];i++);
  21. if(i<n-1)
  22. return false;
  23. else
  24. return true;
  25. }
  26. boolean verificardesc() // Verifica Ordenamiento Descendente
  27. {
  28. for(i=0;i<n-1 && A[i]>=A[i+1];i++);
  29. if(i<n-1)
  30. return false;
  31. else
  32. return true;
  33. }
  34. boolean buscarasc(int x) // Busqueda Ascendente
  35. {
  36. for(i=0;i<n && A[i]<x;i++);
  37. if(i<n && A[i]==x)
  38. return true;
  39. else
  40. return false;
  41. }
  42. boolean buscardesc(int x) // Busqueda Descendente
  43. {
  44. for(i=0;i<n && A[i]>x;i++);
  45. if(i<n && A[i]==x)
  46. return true;
  47. else
  48. return false;
  49. }
  50. boolean busqueda(int x) // Busqueda Normal
  51. {
  52. for(i=0;i<n && A[i]!=x;i++);
  53. if(i<n)
  54. return true;
  55. else
  56. return false;
  57. }
  58. void consulta() // Recorrer el contenido de vector
  59. {
  60. for(i=0;i<n;i++)
  61. {
  62. S.append(A[i]);
  63. S.append('\n');
  64. }
  65.  
  66. //JOptionPane.showMessageDialog(null,S.toString());
  67. }
  68. @SuppressWarnings("deprecation")
  69. void lectura() // Leer el Vector
  70. {
  71. for(i=0;i<n;i++)
  72. {
  73. try
  74. {
  75. System.out.println("Digite el valor de la posición A["+i+"]: ");
  76. A[i]=Integer.parseInt(entrada.readLine()) ;
  77. }
  78. catch(IOException e) {} ;
  79. }
  80. }
  81. void ordenarasc() // Ordenar Ascendente
  82. {
  83. for(i=0;i<n-1;i++)
  84. for(j=i+1;j<n;j++)
  85. if(A[i]>A[j])
  86. {
  87. aux=A[i];
  88. A[i]=A[j];
  89. A[j]=aux;
  90. }
  91. }
  92. void ordenardesc()// Ordenar Descendente
  93. {
  94. for(i=0;i<n-1;i++)
  95. for(j=i+1;j<n;j++)
  96. if(A[i]<A[j])
  97. {
  98. aux=A[i];
  99. A[i]=A[j];
  100. A[j]=aux;
  101. }
  102. }
  103. }
  104. public class clase_arreglo
  105. {
  106. @SuppressWarnings("deprecation")
  107. public static void main(String args[])
  108. {
  109. int i,opcion,x=0;
  110. int B[]=new int[10];
  111. Random R=new Random();
  112. for(i=0;i<B.length;i++)
  113. B[i]=Math.abs(R.nextInt()%100+1);
  114. String S=new String();
  115. Arreglo Z=new Arreglo(B);
  116. @SuppressWarnings("unused")
  117. String Menu="1. Lectura \n 2. Ordenar Ascendente \n 3. Ordenar Descendente \n 4. Busqueda \n 5. Consulta \n6. Salir \n Seleccion Opcion[1..6]";
  118. do
  119. {
  120. do
  121.  
  122. {
  123. //S=JOptionPane.showInputDialog(null,Menu);
  124.  
  125. System.out.println(" null,Menu ");
  126. try
  127. {
  128. opcion=Integer.parseInt(S);
  129. }
  130. {
  131. opcion=0; // Error de Conversion
  132. }
  133. }while(opcion<1 || opcion>6);
  134. if(opcion!=6)
  135. {
  136. switch(opcion)
  137. {
  138. case 1:Z.lectura();
  139. break;
  140. case 2:Z.ordenarasc();
  141. break;
  142. case 3:Z.ordenardesc();
  143. break;
  144. case 4:
  145. try
  146. {
  147. System.out.println(" Numero a Buscar ");
  148. x=Integer.parseInt(entrada.readLine()) ;
  149. }
  150. catch(IOException e) {} ;
  151.  
  152. //x=Integer.parseInt(
  153. //JOptionPane.showInputDialog(null," Numero a Buscar "));
  154.  
  155. if(!Z.buscarasc(x) && !Z.buscardesc(x) && !Z.busqueda(x))
  156.  
  157. //JOptionPane.showMessageDialog(null,x+" No existe");
  158. System.out.println( x+" No existe");
  159. else
  160. //JOptionPane.showMessageDialog(null,x+" Existe");
  161. System.out.println( x+" existe");
  162. break;
  163. case 5: Z.consulta();
  164. }
  165. }
  166. }while(opcion!=6);
  167. System.exit(0);
  168. }
  169. }
  170.  

lo que esta comentado era el codigo  antiguo, quisiera mostrar el menú, ya que no lo muestra, con java .io como lo hago, se que  el codigo esta muy largo por si despues queda falta de especificaciones, .espero alguna sugerencia, muchas gracias


Título: Re: pasar un programa de java.swit a java.io
Publicado por: javier210186 en 17 Marzo 2013, 03:54 am
para los amigos que de pronto se pasan por acá que ya he visto bastantes, ya he logrado solucionarlo, tal vez le pueda servir

Código
  1.   import java.io.*;
  2. import java.util.*;
  3.  
  4. class Arreglo
  5. {
  6. int A[]=new int[100];
  7. int aux;
  8. int i,j,n;
  9. public Arreglo(int B[]) //Constructor
  10. {
  11. if(B.length<=100)
  12. n=B.length;
  13. else
  14. n=100;
  15. for(i=0;i<n;i++)
  16. A[i]=B[i];
  17. }
  18. boolean verificarasc() // Verifica Ordenamiento ascendente
  19. {
  20. for(i=0;i<n-1 && A[i]<=A[i+1];i++);
  21. if(i<n-1)
  22. return false;
  23. else
  24. return true;
  25. }
  26. boolean verificardesc() // Verifica Ordenamiento Descendente
  27. {
  28. for(i=0;i<n-1 && A[i]>=A[i+1];i++);
  29. if(i<n-1)
  30. return false;
  31. else
  32. return true;
  33. }
  34. boolean buscarasc(int x) // Busqueda Ascendente
  35. {
  36. for(i=0;i<n && A[i]<x;i++);
  37. if(i<n && A[i]==x)
  38. return true;
  39. else
  40. return false;
  41. }
  42. boolean buscardesc(int x) // Busqueda Descendente
  43. {
  44. for(i=0;i<n && A[i]>x;i++);
  45. if(i<n && A[i]==x)
  46. return true;
  47. else
  48. return false;
  49. }
  50. boolean busqueda(int x) // Busqueda Normal
  51. {
  52. for(i=0;i<n && A[i]!=x;i++);
  53. if(i<n)
  54. return true;
  55. else
  56. return false;
  57. }
  58. void consulta() // Recorrer el contenido de vector
  59. {
  60. for(i=0;i<n;i++)
  61. {
  62. S.append(A[i]);
  63. S.append('\n');
  64. }
  65.  
  66. //JOptionPane.showMessageDialog(null,S.toString());
  67. }
  68. @SuppressWarnings("deprecation")
  69. void lectura() // Leer el Vector
  70. {
  71. for(i=0;i<n;i++)
  72. {
  73. try
  74. {
  75. System.out.println("Digite el valor de la posición A["+i+"]: ");
  76. A[i]=Integer.parseInt(entrada.readLine()) ;
  77. }
  78. catch(IOException e) {} ;
  79. }
  80. }
  81. void ordenarasc() // Ordenar Ascendente
  82. {
  83. for(i=0;i<n-1;i++)
  84. for(j=i+1;j<n;j++)
  85. if(A[i]>A[j])
  86. {
  87. aux=A[i];
  88. A[i]=A[j];
  89. A[j]=aux;
  90. }
  91. }
  92. void ordenardesc()// Ordenar Descendente
  93. {
  94. for(i=0;i<n-1;i++)
  95. for(j=i+1;j<n;j++)
  96. if(A[i]<A[j])
  97. {
  98. aux=A[i];
  99. A[i]=A[j];
  100. A[j]=aux;
  101. }
  102. }
  103. }
  104.  
  105.  
  106.  
  107. public class clase_arreglo
  108. {
  109. @SuppressWarnings("deprecation")
  110.  
  111.  
  112.  
  113. public static void main(String args[]) throws IOException
  114. {
  115. int i,opcion,x=0;
  116. int B[]=new int[10];
  117. Random R=new Random();
  118. for(i=0;i<B.length;i++)
  119. B[i]=Math.abs(R.nextInt()%100+1);
  120. Arreglo Z=new Arreglo(B);
  121. String Menu="1. Lectura \n 2. Ordenar Ascendente \n 3. Ordenar Descendente \n 4. Busqueda \n 5. Consulta \n6. Salir \n Seleccion Opcion[1..6]";
  122.  
  123. do
  124.  
  125. {
  126.     System.out.print(Menu );
  127.           try
  128.   {
  129. System.out.println(" Por favor Digite un numero: ");
  130. opcion=Integer.parseInt(entrada.readLine()) ;
  131.   }
  132.  
  133.        {
  134.            opcion=0; // Error den Conversion
  135.             }
  136.           }
  137. while(opcion<1 || opcion>6);
  138. if(opcion!=6)
  139. {
  140. switch(opcion)
  141. {
  142. case 1:Z.lectura();
  143. break;
  144. case 2:Z.ordenarasc();
  145. break;
  146. case 3:Z.ordenardesc();
  147. break;
  148. case 4:
  149. try
  150. {
  151. System.out.println(" Numero a Buscar ");
  152. x=Integer.parseInt(entrada.readLine()) ;
  153. }
  154. catch(IOException e) {} ;
  155.  
  156.  
  157.  
  158. if(!Z.buscarasc(x) && !Z.buscardesc(x) && !Z.busqueda(x))
  159.  
  160.  
  161. System.out.println( x+" No existe");
  162. else
  163.  
  164. System.out.println( x+" existe");
  165. break;
  166. case 5: Z.consulta();
  167. case 6: System.exit(0);
  168. }
  169. }
  170. }
  171. }
  172.