Results 1 to 8 of 8
  1. #1
    Adithya is offline Member
    Join Date
    Jun 2014
    Posts
    24
    Rep Power
    0

    Default How to shuffle ?

    Java Code:
    
    
    public class sample {
    	public static void main(){
    		
    		String[] name = new String[4];
    	         name[0]= john;         //error
    		name[1]= fred;          //error    
    		name[2]=bil;              //error
          		name[3]=sally;           //error
    		
    		
    		
    		String[] names = {name[0], name[1], name[2], name[4]}; 
    		Collections.shuffle( Arrays.asList(names)); 
    		for (int i = 0; i < names.length; i++) 
    		{ 
    		    System.out.println(names[i]); 
    		}
    		
    	   
    	}
    	
    
    }

    i shown above my error..i want to use this module in my project.kindly find the bugs

  2. #2
    tajinder is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default Re: How to shuffle ?

    At line 12 : You should use name[3] instead of name[4]
    At line 2: you should pass Argument in public static void main method
    like this
    public static void main(String[] args)
    and you should import Collections and Arrays in your project.
    Thats it . It will work.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to shuffle ?

    Hi,

    when you want to specify a specific string, you have to put the string content in quotes e.g.
    name[0]="john";

    Then your code will compile. And also be aware of the error mentioned by tajinder: your name array has a size of 4 so the single items will be from 0 to 3. Accessing number 4 will give you a runtime exception.

    Konrad

  4. #4
    Adithya is offline Member
    Join Date
    Jun 2014
    Posts
    24
    Rep Power
    0

    Default Re: How to shuffle ?

    Java Code:
    import java.util.Arrays;
    
    import java.util.Collections;
    
    
    
    public class sample {
    	
    
    	public static void main(String[] args){
    		
    		String[] name = new String[4];
    	    name[0]="john";
    	    name[1]="fred";
    		name[2]="bil";
    		name[3]="sally";
    		
    		
    		
    		String[] names = {name[0], name[1], name[2], name[3]}; 
    		Collections.shuffle( Arrays.asList(names)); 
    		for (int i = 0; i < names.length; i++) 
    		{ 
    		    System.out.println(names[i]); 
    		}
    		
    	   
    	}
    	
    }
    still error on same place

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: How to shuffle ?

    What error?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,050
    Rep Power
    6

    Default Re: How to shuffle ?

    That last code compiles and runs just fine. Methinks in your haste you forgot to recompile it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Adithya is offline Member
    Join Date
    Jun 2014
    Posts
    24
    Rep Power
    0

    Default Re: How to shuffle ?

    Quote Originally Posted by Tolls View Post
    What error?

    thank u. now program compiling without error.
    Last edited by Adithya; 06-16-2014 at 01:02 PM.

  8. #8
    pbj
    pbj is offline Member
    Join Date
    Jun 2014
    Posts
    15
    Rep Power
    0

    Default Re: How to shuffle ?

    adith,
    Your code as is it stands seems kind of wasteful, first:
    Java Code:
    String[] name = new String[4];
            name[0]="john";
            name[1]="fred";
            name[2]="bil";
            name[3]="sally";
             
             
             
            String[] names = {name[0], name[1], name[2], name[3]};
    can simply be replaced with:
    Java Code:
    String names[] = {"John", "Fred", "Bill", "Sally"};
    then after you shuffle your array, just use a for-each loop to print out each string
    like so:
    Java Code:
    for (String s : names) System.out.println(s);
    Then your overall code will be much more to-the-point and have less room for error.

Similar Threads

  1. what is wrong with this shuffle method??
    By javaa in forum New To Java
    Replies: 3
    Last Post: 09-06-2012, 02:30 AM
  2. File List shuffle
    By killerwake in forum New To Java
    Replies: 3
    Last Post: 03-12-2011, 10:59 PM
  3. Deck Shuffle
    By Cragsterboy in forum New To Java
    Replies: 1
    Last Post: 12-13-2010, 04:23 PM
  4. How do I shuffle an arraylist?
    By frasifrasi in forum New To Java
    Replies: 2
    Last Post: 07-16-2008, 11:29 PM
  5. How to Shuffle a particular String
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-06-2008, 07:38 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
  •