Foro de elhacker.net

Programación => Java => Mensaje iniciado por: monsefoster en 19 Noviembre 2009, 02:32 am



Título: Matriz de Strings, tengo un pequeño problema
Publicado por: monsefoster en 19 Noviembre 2009, 02:32 am
Hola chicos, Tengo que hacer algo como esto...

Un Granjero tiene 3 parcelas divididas en partes iguales, con siembra de papas,tomates y zanahorias...
Calcular la cantidad de papas, tomates y zanahorias en total de las 3 parcelas...

Los datos se ingresarian asi "tomate 50 kgs", etc

            TomatesPapasZanahorias
Parcela 1
Parcela 2
Parcela 3
Intenté hacer algo como esto, pero en "calcular" me da "NumberException", ya verifiqué que lo que queda en "shorty" es solo el numero. El problema esta en la linea "sht=Integer.parseInt(shorty);"

Código
  1. package clase_apoyo;
  2. import java.io.*;
  3.  
  4. public class apoyo {
  5. /**
  6. * Method llenarmatriz
  7. *
  8. *
  9. * @return
  10. *
  11. */
  12. public static String[][] llenarmatriz() throws IOException {
  13. String m[][]= new String[2][2];
  14. int i,j;
  15. for (i=0;i<m.length;i++){
  16. System.out.println ("Parcela "+(i+1)+" ");
  17. for (j=0;j<m[0].length;j++){
  18. System.out.println ("Producto "+(j+1)+" y Peso (Sin Kg)");
  19. System.out.flush ();
  20. m[i][j] = en.readLine ();
  21. }
  22. }
  23. return m;
  24. }
  25.  
  26. /**
  27. * Method mostrar
  28. *
  29. *
  30. */
  31. public static void mostrar(String m[][]) {
  32. int i,j;
  33. for (i=0;i<m.length;i++){
  34. System.out.println ("Los datos almacenados son: ");
  35. System.out.println ("Parcela "+(i+1)+" ");;
  36. for (j=0;j<m[0].length;j++){
  37. System.out.println (" "+m[i][j]+" ");
  38.  
  39. }
  40. }
  41. }
  42.  
  43. /**
  44. * Method calcular
  45. *
  46. *
  47. */
  48. public static void calcular(String m[][]) {
  49. int i,j,k,p=0;
  50. int sum,sht=0;
  51. String choice;
  52. String shorty=null,title=null;
  53. for (i=0;i<m[0].length;i++){
  54. sum=0;
  55. for (j=0;j<m.length;j++){
  56. choice=m[j][i];
  57. for (k=0;k<choice.length();k++){
  58. if (choice.charAt(k)==' '){
  59. p=k;
  60. k=choice.length();
  61. title=choice.substring(0,p);
  62. }
  63. }
  64.  
  65. shorty=choice.substring(p);
  66. sht=Integer.parseInt(shorty);
  67. sum+=sht;
  68. }
  69. System.out.println (title+" total = "+sht);
  70. }
  71. }
  72. }
  73.  



Título: Re: Matriz de Strings, tengo un pequeño problema
Publicado por: Leyer en 19 Noviembre 2009, 03:30 am
"NumberException" colocale un trim shorty=choice.substring(p).trim();

Saludos


Título: Re: Matriz de Strings, tengo un pequeño problema
Publicado por: monsefoster en 19 Noviembre 2009, 05:20 am
 ;D Excelente....


Muchisimas GRACIAS!!!!  ;D


Título: Re: Matriz de Strings, tengo un pequeño problema
Publicado por: Debci en 21 Noviembre 2009, 21:56 pm
"NumberException" colocale un trim shorty=choice.substring(p).trim();

Saludos

o un String.valueOf();

Saludos