Results 1 to 14 of 14
  1. #1
    LukaSchnauzer is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Unhappy cmd doing nothing [objects arrays]

    HI, im new at the forum
    I have to random shuffle an array of Card Objects which does the funcion of a deck. Heres the code:
    Java Code:
    public void barajear(){
    			int j;
    		for (int i=0;i<52;i++){
    			j=Baraja.random(51);
    			if (this.mazo[j]==null){
    				this.mazo[j]=this.arreglo[i];
    			}else{
    				--i;
    			}
    		}
    	}
    so bassically theres an array called "arreglo" which has the cards in order and the function "random" its an rng of numbers from 0 to 51.
    what i'm trying to do it's to take the cards from the ordenated array and put them randomly in the other but only if it's empty.
    (the array "mazo" has alredy been initialized with null).
    it worked at first, but now, after compiling succesfully i tried to run it and the cmd just...
    Attachment 6693
    just stays there, i can't do anything: i can't type, just stays that way...
    somebody knows why this is happening i don't know what to do
    I know it is because it loopes, but i don't see how :/
    Thank you and sorry if my english is bad.
    Last edited by LukaSchnauzer; 10-26-2014 at 04:50 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: cmd doing nothing [objects arrays]

    One way to find which loop in looping forever is to add println() statements inside of all the loops that could be bad. The printed output will show you which loop is executing too long.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: cmd doing nothing [objects arrays]

    Shouldn't that be 52 in line #4 instead of 51? (I don't know what a 'Baraja' is though ...)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: cmd doing nothing [objects arrays]

    Here is a past thread from this forum on shuffling. It may be useful.

    Random shuffling


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by jim829 View Post
    Here is a past thread from this forum on shuffling. It may be useful.

    Random shuffling
    That thread is more like an 'hommage' to Donald Knuth ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    LukaSchnauzer is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by JosAH View Post
    Shouldn't that be 52 in line #4 instead of 51? (I don't know what a 'Baraja' is though ...)

    kind regards,

    Jos
    the random function generates numbres from 0 to the parameter, so it should be 51 since the array is marked from 0 to 51.
    a Baraja is a Deck xD sorry it's in spanish

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by LukaSchnauzer View Post
    the random function generates numbres from 0 to the parameter, so it should be 51 since the array is marked from 0 to 51.
    a Baraja is a Deck xD sorry it's in spanish
    Are you sure that is including the upper bound 'parameter'? Show us the implementation of that method so we can be sure ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    LukaSchnauzer is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by JosAH View Post
    Are you sure that is including the upper bound 'parameter'? Show us the implementation of that method so we can be sure ...

    kind regards,

    Jos
    yes i'm sure i tried it with 52 being the parameter but heres the code:

    private static int random(int max) {
    return (int) Math.round(Math.random() * max + 0.5);
    }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: cmd doing nothing [objects arrays]

    Did you add the println() to see what loop was going forever?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    LukaSchnauzer is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by Norm View Post
    Did you add the println() to see what loop was going forever?
    yes i did, and it loops in the last part, the number 51 :/

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by LukaSchnauzer View Post
    yes i did, and it loops in the last part, the number 51 :/
    That's because your random number generator generates numbers in the range 1 ... 51 (it can't generate 0).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    LukaSchnauzer is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by JosAH View Post
    That's because your random number generator generates numbers in the range 1 ... 51 (it can't generate 0).

    kind regards,

    Jos
    you're rigth, how can i make it generate 0 too?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: cmd doing nothing [objects arrays]

    If you want to generate a pseudo random number in the range 0 .. n (inclusive) you can do: (int)(Math.random()*(n+1)) and forget about all the numer juggling you have now in your method.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    LukaSchnauzer is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Re: cmd doing nothing [objects arrays]

    Quote Originally Posted by JosAH View Post
    If you want to generate a pseudo random number in the range 0 .. n (inclusive) you can do: (int)(Math.random()*(n+1)) and forget about all the numer juggling you have now in your method.

    kind regards,

    Jos
    thanks :D it's done

Similar Threads

  1. Arrays of Objects
    By rgru25 in forum New To Java
    Replies: 3
    Last Post: 04-02-2013, 12:30 PM
  2. Arrays for objects
    By mjfitches in forum New To Java
    Replies: 1
    Last Post: 02-23-2013, 10:20 PM
  3. arrays and/ are objects
    By willemjav in forum New To Java
    Replies: 2
    Last Post: 01-02-2013, 04:20 PM
  4. Arrays of Objects
    By superscout in forum New To Java
    Replies: 2
    Last Post: 01-15-2012, 02:16 PM
  5. Objects and Arrays
    By bannow in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •