Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Returning Random Strings from an Array

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Returning Random Strings from an Array

    I'm trying to construct a class named Card that will eventually deal and print a certain number of cards.

    I'm having trouble generating random strings from two arrays called suitArray and faceArray. Im trying to pass the arrays into the nextLine method, but it's not working. How can I make this work?

    import java.util.Random;
     
    class Card{
     
      String suit,face;
     
      String[] suitArray={"hearts","diamonds","clubs","spades"};
      String[] faceArray={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
     
     
      public Card(String s, String f){
        Random gen = new Random();
        s = suit.gen.nextLine(suitArray);
        f = face.gen.nextLine(faceArray); 
      }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Returning Random Strings from an Array

    The constructor is passed arguments and it builds an object based on those arguments, not the other way around. Also, String's don't have a gen field, nor is Random able to pick a random string from an array.

    Also, it doesn't make sense that you're generating a random suit and value when you're passing it to the constructor. Here's some partially-finished code that should help you write this to do what you want it to do:

    import java.util.Random;
     
    public class Card
    {
         String suit,face;
     
         String[] suitArray={"hearts","diamonds","clubs","spades"};
         String[] faceArray={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
     
         /**
          * Generates a card with a random suit and face
          **/
         public Card()
         {
              Random gen = new Random();
              // since we can access arrays via index, let's generate some random numbers for the indices
              suit = suitArray[gen.nextInt() % suitArray.length()];
              face = faceArray[gen.nextInt() % faceArray.length()];
         }
     
         /**
          * Creates a card given a suit and a face
          * @param suit
          * @param face
          */
         public Card(String s, String f)
         {
              // TODO: you're turn. What code should you put here so the Card is created correctly?
         }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    cfmonster (September 8th, 2009)

  4. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Returning Random Strings from an Array

    I took your suggestion of:
    Random gen = new Random();
              // since we can access arrays via index, let's generate some random numbers for the indices
              suit = suitArray[gen.nextInt() % suitArray.length()];
              face = faceArray[gen.nextInt() % faceArray.length()];

    and modified it to:

    Random gen = new Random();
              // since we can access arrays via index, let's generate some random numbers for the indices
              suit = suitArray[gen.nextInt(4)];
              face = faceArray[gen.nextInt(13)];

    to make it work. Also got a few deal methods to work. Thanks for the help!

  5. #4
    Junior Member
    Join Date
    Sep 2009
    Location
    Bangalore.India
    Posts
    9
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Returning Random Strings from an Array

    Quote Originally Posted by cfmonster View Post
    I'm trying to construct a class named Card that will eventually deal and print a certain number of cards.

    I'm having trouble generating random strings from two arrays called suitArray and faceArray. Im trying to pass the arrays into the nextLine method, but it's not working. How can I make this work?

    import java.util.Random;
     
    class Card{
     
      String suit,face;
     
      String[] suitArray={"hearts","diamonds","clubs","spades"};
      String[] faceArray={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
     
     
      public Card(String s, String f){
        Random gen = new Random();
        s = suit.gen.nextLine(suitArray);
        f = face.gen.nextLine(faceArray); 
      }
    }

    Try the following code

     
    class Card{
     
      private static String[] faceArray={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
     
      public static void generateRandom(){	
    	  int minimum = 0;
    	  int maximum = 13;
    	  int randomNum = minimum + (int)(Math.random()* maximum);
          System.out.println(faceArray[randomNum]); 
      }
     
      public static void main(String[] args) {
    	  generateRandom();
      }
    }
    Last edited by vivekmk; September 8th, 2009 at 11:16 PM.

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 12th, 2009, 01:51 AM
  3. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM
  4. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM