Results 1 to 12 of 12

Thread: I have this weird problem..
- 08-13-2011, 03:31 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
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.
-
Your array is fine, but the problem is here:
Java Code:System.out.println("New:" +enemyPieces[xy]);
- 08-13-2011, 03:40 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
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 " "..
-
- 08-13-2011, 03:48 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
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]); }
- 08-13-2011, 04:53 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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
Java Code:for loop to fill your array for loop to shuffle your array for loop to print your array
JosBuild a wall around Donald Trump; I'll pay for it.
-
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]); } } }
- 08-14-2011, 02:14 AM #8
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
I cannot understand what you are trying to say.. Any simpler explanations please? Thanks!
- 08-14-2011, 04:48 AM #9
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
I think that JosAH did not see the closing bracket on this line:
Java Code:enemyPieces[my] = "enemy";}
- 08-14-2011, 04:53 AM #10
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
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";} }
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; }
- 08-14-2011, 05:02 AM #11
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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); } }
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
- 08-14-2011, 05:29 AM #12
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
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]); } } }
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 05:34 AM.
Similar Threads
-
Weird problem with 36 and 39?
By jh7468 in forum New To JavaReplies: 3Last Post: 02-06-2011, 09:01 PM -
Weird problem with distributing .jar
By Cynot in forum New To JavaReplies: 4Last Post: 06-07-2010, 01:03 PM -
Weird problem with JSTL
By Diego_Dalmasso in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-02-2010, 03:51 PM -
weird problem
By GPB in forum New To JavaReplies: 2Last Post: 02-28-2010, 01:04 PM -
Weird msdos problem
By dudejonne in forum New To JavaReplies: 6Last Post: 11-02-2009, 09:39 PM
Bookmarks