Found Bug but can't understand it

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Found Bug but can't understand it

 
0
  #1
Apr 17th, 2009
hi,

I have to do a program that generates 'n' different combinations of 7 different numbers between 1 and 49, so I did this:
  1. import java.util.*;
  2. public class Totoloto {
  3.  
  4. public static void main(String[] args) {
  5. System.out.print("Quantas apostas?");
  6. Scanner s = new Scanner(System.in);
  7. int n=s.nextInt();
  8. int[][] apostas = geraApostas(n);
  9. for(int i=0;i<apostas.length;i++)
  10. {
  11. System.out.print("[");
  12. for(int a=0;a<7;a++)
  13. {
  14. System.out.print(apostas[i][a]);
  15. if(a!=6)
  16. System.out.print(", ");
  17. }
  18. System.out.println("]");
  19. }
  20. }
  21.  
  22. private static int[] geraAposta(){
  23. int[] aposta = new int[7];
  24. for(int i=0;i<7;i++)
  25. {
  26. int num;
  27. Boolean existe;
  28. do{
  29. existe=false;
  30. num=(int) (Math.random()*49+1);
  31. for(int a=0;a<7;a++)
  32. if(aposta[a]==num)
  33. existe=true;
  34. }while(existe);
  35. aposta[i]=num;
  36. }
  37. return aposta;
  38. }
  39.  
  40. private static int[][] geraApostas(int n){
  41. int[][] apostas = new int[7][n];
  42. System.out.println(n + "" + apostas.length);
  43. for(int i=0;i<n;i++)
  44. {
  45. int[] aposta;
  46. Boolean existe;
  47. do{
  48. existe=false;
  49. aposta=geraAposta();
  50. for(int a=0;a<n;a++)
  51. if(apostas[n]==aposta)
  52. existe=true;
  53. }while(existe);
  54. apostas[i]=aposta;
  55. }
  56. return apostas;
  57. }
  58. }

When I run it:
  1. Quantas apostas?9
  2. 97
  3. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
  4. at Totoloto.geraApostas(Totoloto.java:54)
  5. at Totoloto.main(Totoloto.java:12)

This happens cecause, for int[][] apostas = new int[7][n];, apostas.lenght equals 7! how can I obtain 'n' with just the array and the length function?

thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Found Bug but can't understand it

 
0
  #2
Apr 18th, 2009
You are very close there: int[][] apostas = new int[n][7]; You want 'n' elements that each contain an array of 7 elements. Your outer loop for(int i=0;i<n;i++) then denotes that first dimension, so aposta[i]=geraAposta();
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: Found Bug but can't understand it

 
0
  #3
Apr 19th, 2009
Originally Posted by Ezzaral View Post
You are very close there: int[][] apostas = new int[n][7]; You want 'n' elements that each contain an array of 7 elements. Your outer loop for(int i=0;i<n;i++) then denotes that first dimension, so aposta[i]=geraAposta();
thank you very much! problem solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC