Results 1 to 12 of 12
Like Tree4Likes
  • 1 Post By saber210
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By saber210

Thread: I have this weird problem..

  1. #1
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default I have this weird problem..

    First of all, I want to put strings on my array.. My array size will be 27.. The data must have 6 " " and the rest will have "enemy".. But my problem when i shuffle the data on the array sometimes it consist of 5 " " or even 7 " "

    The code is here:

    Java Code:
    String [] enemyPieces= new String[27];
    
    for(int my = 0; my <= 26; my++){
                    if(my < 6 ){
                        enemyPieces[my] = "";
                    }else{
                        enemyPieces[my] = "enemy";}
                }
    
                for (int xy = 0; xy < enemyPieces.length ;xy++){
                int randomPosition = randomPos.nextInt(enemyPieces.length);
                String temp = enemyPieces[xy];
                enemyPieces[xy] = enemyPieces[randomPosition];
                enemyPieces[randomPosition] = temp;
    
                System.out.println("New:" +enemyPieces[xy]);
                }

    I dont know what is the problem on my code.
    JeffGrigg likes this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your array is fine, but the problem is here:

    Java Code:
    System.out.println("New:" +enemyPieces[xy]);
    You're printing out the array position before the "shuffle" has completed which will give you false information. Instead check the array contents after your pseudo-shuffle by creating another for loop after your for loop above, and printing out all the array values in this for loop.
    JeffGrigg likes this.

  3. #3
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    I just put that code to check the items on the array list.. But it is always same.. It produce sometimes five " " and sometimes seven " "..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by saber210 View Post
    I just put that code to check the items on the array list.. But it is always same.. It produce sometimes five " " and sometimes seven " "..
    Let's see the new code.
    JeffGrigg likes this.

  5. #5
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    Here is the code:

    Java Code:
                for(int my = 0; my <= 26; my++){
                    if(my <= 5 ){
                        enemyPieces[my] = "";
                    }else{
                        enemyPieces[my] = "enemy";}
                }
    
                for (int xy = 0; xy < enemyPieces.length ;xy++){
                int randomPosition = randomPos.nextInt(enemyPieces.length);
                String temp = enemyPieces[xy];
                enemyPieces[xy] = enemyPieces[randomPosition];
                enemyPieces[randomPosition] = temp;
    
    
                }
    
                for (int mi = 0; mi < enemyPieces.length ;mi++){
                System.out.println("New:" +enemyPieces[mi]);
                }

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

    Default

    Quote Originally Posted by saber210 View Post
    Here is the code:

    Java Code:
                for(int my = 0; my <= 26; my++){
                    if(my <= 5 ){
                        enemyPieces[my] = "";
                    }else{
                        enemyPieces[my] = "enemy";}
                }
    
                for (int xy = 0; xy < enemyPieces.length ;xy++){
                int randomPosition = randomPos.nextInt(enemyPieces.length);
                String temp = enemyPieces[xy];
                enemyPieces[xy] = enemyPieces[randomPosition];
                enemyPieces[randomPosition] = temp;
    
    
                }
    
                for (int mi = 0; mi < enemyPieces.length ;mi++){
                System.out.println("New:" +enemyPieces[mi]);
                }
    Your curly bracketing is goofy; closely inspect your own code; it should be similar to this:

    Java Code:
    for loop to fille your array
    for loop to shuffle your array
    for loop to print your array
    Now your code seems to be like this:

    Java Code:
    for loop to fill your array
       for loop to shuffle your array
       for loop to print your array
    kind regards

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So to flesh out the program, are you telling me that this code gives you a variable number of array items that hold spaces?

    Java Code:
    import java.util.Random;
    
    public class Foo001 {
    
       public static void main(String[] args) {
          String[] enemyPieces = new String[27];
          Random randomPos = new Random();
          for (int my = 0; my <= 26; my++) {
             if (my <= 5) {
                enemyPieces[my] = "";
             } else {
                enemyPieces[my] = "enemy";
             }
          }
    
          for (int xy = 0; xy < enemyPieces.length; xy++) {
             int randomPosition = randomPos.nextInt(enemyPieces.length);
             String temp = enemyPieces[xy];
             enemyPieces[xy] = enemyPieces[randomPosition];
             enemyPieces[randomPosition] = temp;
    
          }
    
          for (int mi = 0; mi < enemyPieces.length; mi++) {
             System.out.println("New:" + enemyPieces[mi]);
    
          }
       }
    }
    Are you 100% sure about that? I urge you to double and even triple check this.

  8. #8
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    I cannot understand what you are trying to say.. Any simpler explanations please? Thanks!

  9. #9
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    I think that JosAH did not see the closing bracket on this line:
    Java Code:
                        enemyPieces[my] = "enemy";}
    So I think I agree with Fubarable that the code you posted appears to work correctly every time.

  10. #10
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    On the first part of the code works fine.. It produce 6 blank spaces and the other 21 are enemies..

    This is the first code:

    Java Code:
                for(int my = 0; my <= 26; my++){
                    if(my <= 5 ){
                        enemyPieces[my] = "";
                    }else{
                        enemyPieces[my] = "enemy";}
                }
    I think this is the problem.. Sometimes it produce 7 blank spaces or sometimes 5 blank spaces.. but it must be 6 blank spaces..

    Java Code:
     for (int xy = 0; xy < 26 ;xy++){
                int randomPosition = randomPos.nextInt(enemyPieces.length);
                String temp = enemyPieces[xy];
                enemyPieces[xy] = enemyPieces[randomPosition];
                enemyPieces[randomPosition] = temp;
    
    
                }
    ANy fix for this? Thank you..

  11. #11
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    I run this code:
    Java Code:
    import java.util.Random;
    
    public class WeirdProblem {
    
    	public static void main(String[] args) {
    		String[] enemyPieces = new String[27];
    		Random randomPos = new Random();
    
    		for (int my = 0; my <= 26; my++) {
    			if (my <= 5) {
    				enemyPieces[my] = "";
    			} else {
    				enemyPieces[my] = "enemy";
    			}
    		}
    
    		for (int xy = 0; xy < enemyPieces.length; xy++) {
    			int randomPosition = randomPos.nextInt(enemyPieces.length);
    			String temp = enemyPieces[xy];
    			enemyPieces[xy] = enemyPieces[randomPosition];
    			enemyPieces[randomPosition] = temp;
    		}
    
    		int empties = 0;
    		for (int mi = 0; mi < enemyPieces.length; mi++) {
    			System.out.println("New:" + enemyPieces[mi]);
    			if ("".equals(enemyPieces[mi]))
    				++empties;
    		}
    		System.out.println("Number of empty spaces: " + empties);
    	}
    
    }
    I get this result:
    Java Code:
    New:
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:
    New:
    New:enemy
    New:enemy
    New:
    New:
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:enemy
    New:
    New:enemy
    New:enemy
    Number of empty spaces: 6
    I get "Number of empty spaces: 6" every time.

  12. #12
    saber210 is offline Member
    Join Date
    Jul 2011
    Posts
    15
    Rep Power
    0

    Default

    This if for board game which is 9 by 8 board and I used this code to display it..

    Java Code:
            for(int i = 0; i <= 7; i++)
            {
                for(int i1 = 0; i1 <= 8; i1++)
                {
                    if (i == 0){
                    Board[i][i1].setText(enemyPieces[i1]);
                    }else
                    if (i == 1){
                    Board[i][i1].setText(enemyPieces[i1+9]);
                    }else
                    if (i == 2){
                    Board[i][i1].setText(enemyPieces[i1+17]);
                    }
                }
    
            }
    Is there any problem on this?

    EDIT: I got it now,, the problem is on how i show the spaces and the enemies.. Thanks a lot.
    Last edited by saber210; 08-14-2011 at 04:34 AM.
    JeffGrigg likes this.

Similar Threads

  1. Weird problem with 36 and 39?
    By jh7468 in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 08:01 PM
  2. Weird problem with distributing .jar
    By Cynot in forum New To Java
    Replies: 4
    Last Post: 06-07-2010, 12:03 PM
  3. Weird problem with JSTL
    By Diego_Dalmasso in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-02-2010, 02:51 PM
  4. weird problem
    By GPB in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 12:04 PM
  5. Weird msdos problem
    By dudejonne in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 08:39 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
  •