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?
Code :
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);
}
}
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:
Code :
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?
}
}
Re: Returning Random Strings from an Array
I took your suggestion of:
Code :
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:
Code :
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!
Re: Returning Random Strings from an Array
Quote:
Originally Posted by
cfmonster
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?
Code :
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
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();
}
}