943,628 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 443
  • Java RSS
Apr 17th, 2009
0

Found Bug but can't understand it

Expand Post »
hi,

I have to do a program that generates 'n' different combinations of 7 different numbers between 1 and 49, so I did this:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Apr 18th, 2009
0

Re: Found Bug but can't understand it

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();
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Apr 19th, 2009
0

Re: Found Bug but can't understand it

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help in writing a litle program for a friend
Next Thread in Java Forum Timeline: Java app to send an SMS via DKU-5 serial cable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC