0

I was learning about arrays today in school, and I am trying to do this problem, but cannot figure it

// Fortune Teller

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

public class FortuneTeller extends JFrame
    implements ActionListener
{
  private static final EasySound ding = new EasySound("ding.wav");

  // Declare an array of "fortunes" (strings):
  ___________________________________________
  ...

  private JTextField display;

  public FortuneTeller()
  {
    super("Fortune Teller");

    display = new JTextField("  Press \"Next\" to see your fortune...", 25);
    display.setBackground(Color.WHITE);
    display.setEditable(false);

    JButton go = new JButton("Next");
    go.addActionListener(this);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(display);
    c.add(go);
  }

  public void actionPerformed(ActionEvent e)
  {
    // Pick and display a random fortune:

    ___________________________________________
    ...

    display.setText("  " + __________________ );
    ding.play();
  }

  public static void main(String[] args)
  {
    JFrame window = new FortuneTeller();
    window.setBounds(300, 300, 300, 100);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(false);
    window.setVisible(true); 
  }
}

I am trying to fill in the blanks.

For the first one,, would it just be just be String [] Fortune;?

But how would I end up initializing it?

And then for the second part, what would I have to do?

Please help as I am extremely lost.

1

2 Answers 2

3

Here are some hints to help you (without actually doing your homework for you).

You can create and populate an array of strings in one line using the following syntax:

String[] colours = {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"};

You can generate a random number between zero and x (inclusive) using the following syntax:

int randomNumber = Math.random() * (x + 1);

You can select a random string from an array with the following syntax:

String randomColour = colours[Math.random() * colours.length];
Sign up to request clarification or add additional context in comments.

Comments

2

As found by the simple Google search "How to declare an array in Java":

String[] fortune = {"You're going to be right","You're going to die","You're going to find a cat"};

This also initializes the array.

Then to get the an element of the array, you do:

fortune[1];

//Returns: You're going to die

For this though, I'd do a random number generator to pick a number between 0 and the length of the array:

int value = Math.random() * fortune.length; 
fortune[value]; 

3 Comments

Do you know how to choose a random element from the list? I was trying to find stuff and got the following: int idx = new Random().nextInt(fruits.length); String random = (fruits[idx]); display.setText(" " + random );
@ConfusedCoder I showed you how to get a random element.
Oh. Didn't that there embarrassment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.