0

I have to make the three arrays in my code (shown below) to print randomly. So for "Today:" and "Tomorrow" print a random good or bad comment. For your lucky and not so lucky days, print a random date and a random good comment for the lucky day and random bad comment for the not so lucky day. I don't know how to pick randomly from string arrays. I have this due tomorrow and I just started coding a few months ago so im not sure how I would go about doing this.

Today: You will meet an old friend! Tomorrow: Trust your intuition. The universe is guiding your life. Your lucky day for 2016 is November 4: You will win the jackpot. Your not so lucky day for 2016 is January 24: Power failure day

This is my code:

String Month[]={"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
String goodcomment[]={"None of the secrets of success will work unless you do","Today is a lucky day for those who remain cheerful and optimistic","You were born with the skill to communicate with people easily","The first step to better times is to imagine them","Trust your intuition. The Universe is your guiding light","It doesnt matter. Who is without a flaw?","Your happiness is interwined with your outlook on life","The secret of getting ahead is getting started", "Failure will never overtake you if my determination to succeed is strong enough.","What you do today can improve all your tomorrows.","In order to succeed, you must first believe that you can.","Always do your best. What you plant now, you will harvest later."};
String badcomment[]={"Power failure today","You'll miss the bus","Too many things to do and not enough time","University entranced denied","You'll lose your phone","You'll miss your date","You'll forget someone close to you's birthday", "Today is a disastrous day. If you can’t beat ’em, join ’em","You will meet someone next week that will bring negativity into your life","Don't step on a crack","You will wake up sad tomorrow","Your car will break down"};
bluebarry
  • 11
  • 4
  • To select a certain string in a string array do `yourStringToDisplay = arrayName[someIndexValue]`, and to pick a random one you can do `(int) Math.random() * arrayName.length,` try these and see if they work. – Riley Carney Nov 12 '15 at 03:32
  • 2
    Possible duplicate of [Generating random integers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java) – Riley Carney Nov 12 '15 at 03:35

2 Answers2

1

You can use the Random class for this. The method nextInt(int bount) will return a random integer from 0 to bound-1.

So to randomly select a string from an array, you can do:

Random random = new Random();
int randomIdx = random.nextInt(goodComment.length);
System.out.println(goodComment[randomIdx]);
tixopi
  • 486
  • 3
  • 14
0

Solution will be obtaining selecting a random number from 0 or 1 to select randomly from good or bad comment (i.e. 0 – good comment and 1 – bad comment). Then selecting a comment randomly from the selected array by obtaining a random number from 0 to (length of the selected array – 1) in order to indicate the index of the printing comment. You can use the approach suggested by tixopi to generate the random number.

private String goodcomment[] = { "None of the secrets of success will work unless you do",
        "Today is a lucky day for those who remain cheerful and optimistic"};
private String badcomment[] = { "Power failure today", "You'll miss the bus", "Too many things to do and not enough time",
        "University entranced denied", "You'll lose your phone", "You'll miss your date"};

private Random indexGenerator = new Random();

public void printCommentWithPrefix(String prefix) {
    System.out.println(prefix + " : " + selectRandomGoodOrBadComment());
}

private String selectRandomGoodOrBadComment() {
    String randomGoodOrBadComment;
    if (selectEitherGoodOrBad() == 0) {
        randomGoodOrBadComment = selectAComment(goodcomment);
    } else {
        randomGoodOrBadComment = selectAComment(badcomment);
    }
    return randomGoodOrBadComment;
}

private int selectEitherGoodOrBad() {
    int goodOrBadIndicator = indexGenerator.nextInt(2);
    return goodOrBadIndicator;
}

private String selectAComment(String[] comment) {
    int indexOfSelectedComment = indexGenerator.nextInt(comment.length);
    String selectedComment = comment[indexOfSelectedComment];
    return selectedComment;
}
Community
  • 1
  • 1
IsuruJ
  • 16
  • 3