Results 1 to 4 of 4
  1. #1
    Cubswin is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default displaying a desired out put

    Hi im trying get a output that looks like this:
    The deck is:
    r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13
    b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 b11 b12 b13
    g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 g11 g12 g13
    y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13
    After shuffling, the deck is:
    r4 r5 r8 r2 b12 g6 y4 y7 r9 g2 y6 g13 y2
    y11 r3 g1 b2 r10 b4 b7 g8 r11 g11 y3 r12 g4
    b13 r7 g3 b6 y13 g7 b9 r1 g9 b3 g10 g12 y8
    y1 b1 g5 y10 y5 r13 r6 b10 y9 b11 b8 b5 y12

    this is the code ive done so far
    Java Code:
    import java.util.Random;
    
    
    public class ArrayDriver {
    
    /*
     * CS 107: Spring 2011
     * Friday, March 11, 2011
     * All about arrays.
     */
    	
    	static Random randomNumberGenerator = new Random(1);  
    	  
    
    	
    	int size = 52;
    	//Square[] deck = new Square[size];
    
    	public static void main(String[] args) {
    		
    		ArrayDriver ad = new ArrayDriver();
    		
    		// Declaring an integer (int) array
    		
    		int size = 10;
    		int[] IntArray = new int[size];
    		
    		
    
    		String[][] String2DArray = new String[4][13];
    		
    		
    		// lets assign some string names to array slots
    		for (int i = 0; i < String2DArray.length; i++)
    			for (int j = 0; j<String2DArray[i].length; j++)
    				String2DArray[i][j] = "R"+((i+1)*(j+1));
    
    		System.out.println();
    		
    		// displaying  the 2 D array with new values
    		for (int i = 0; i < String2DArray.length; i++){
    			for (int j = 0; j<String2DArray[i].length; j++)
    				System.out.print(String2DArray[i][j]+"\t");
    			System.out.println();
    			}
    		
    		// shuffle the 2 D array
    		shuffle(String2DArray);
    		
    		
    		System.out.println();
    		// displaying  the 2 D array after shuffling
    		for (int i = 0; i < String2DArray.length; i++){
    			for (int j = 0; j<String2DArray[i].length; j++)
    				System.out.print(String2DArray[i][j]+"\t");
    			System.out.println();
    			}
    		
    		
    
    		
    		
    		
    
    		
    	}
    	
    	
    		
    	public static void shuffle(String[][] a2DStringArray ){
    		String temp;
    		int row, column;
    		for (int i = 0; i < a2DStringArray.length; i++){
    			for (int j = 0; j<a2DStringArray[i].length; j++){
    				row = randomNumberGenerator.nextInt(a2DStringArray.length);
    				column = randomNumberGenerator.nextInt(a2DStringArray[i].length);
    				temp = a2DStringArray[i][j];
    				a2DStringArray[i][j] = a2DStringArray[row][column];
    				a2DStringArray[row][column] = temp;
    			}
    		}	
    		
    	}
    	
    	
    	
    	
    	
    	
    	
    }
    the output i get is this :

    R1 R2 R3 R4 R5 R6 R7 R8 R9 R10 R11 R12 R13
    R2 R4 R6 R8 R10 R12 R14 R16 R18 R20 R22 R24 R26
    R3 R6 R9 R12 R15 R18 R21 R24 R27 R30 R33 R36 R39
    R4 R8 R12 R16 R20 R24 R28 R32 R36 R40 R44 R48 R52

    R3 R18 R13 R9 R2 R6 R16 R24 R20 R32 R5 R24 R4
    R12 R11 R52 R10 R8 R14 R6 R6 R12 R3 R39 R1 R12
    R8 R30 R27 R44 R22 R7 R28 R36 R16 R15 R18 R4 R33
    R40 R9 R4 R24 R21 R20 R2 R10 R12 R8 R36 R26 R48

    any suggestion would be very appreciated. im i beginner in java.

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    You are filling the whole deck with R values, so that is what's getting shuffled and printed.

    Java Code:
    // lets assign some string names to array slots
    for (int i = 0; i < String2DArray.length; i++)
    	for (int j = 0; j<String2DArray[i].length; j++)
    		String2DArray[i][j] = "R"+((i+1)*(j+1));

    Instead of "R" you need a string that depends in some way on i.

  3. #3
    Cubswin is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    how would i write a string that depend on i?

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Well you have four strings you want to use: "R", "G", "B" and "Y", right? And I am suggesting you associate them with the values i has in that loop code (0, 1, 2 and 3).

    In short you need a simple data structure that associates index numbers with strings. Look at the comment at the start of your class and you will see:

    (1) What that data structure is

    and

    (2) That your assignment is overdue!

Similar Threads

  1. displaying an array
    By exose in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 04:06 PM
  2. Displaying an image
    By BariMutation in forum New To Java
    Replies: 6
    Last Post: 11-12-2010, 02:23 AM
  3. Displaying an image
    By Samari413 in forum New To Java
    Replies: 10
    Last Post: 11-06-2010, 09:56 PM
  4. Can we move a text to a desired place in JTextArea
    By ronaldrandon in forum New To Java
    Replies: 7
    Last Post: 08-09-2010, 06:32 AM
  5. Replies: 20
    Last Post: 10-21-2009, 04:59 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
  •