0

I have the slight problem at the moment with my Java code that I can't pick up a random string from an array I have. A segment of my code is here:

 private static String core;
    {

            String[] insults = new String[15];
            insults[0] = "So's your mum.";
            insults[1] = "I hate you too.";
            insults[2] = "Freak!";
            insults[3] = "Your balls are like peas.";
            insults[4] = "You're so ugly, your birth certificate was an apology letter from the condom factory.";
            insults[5] = "Ooh, take me to the burn unit.";
            insults[6] = "That insult was like your dick- Pathetic.";
            insults[7] = "Your mum looks like a dog. I was brought up not to lie.";
            insults[8] = "Can you look away? It's killing my face...";
            insults[9] = "If you had a house for every good insult you gave me, you'd still be living on the streets!";
            insults[10] = "Shut up, you'll never be the man your mother is.";
            insults[11] = "Shut up, you'll never be the man your mother is.";
            insults[12] = "Oh my God... Was your face squashed in a vice at birth?";
            insults[13] = "I know you are, but what am I?";
            insults[14] = "Oh, okay then...";
             double count = 0;
    };


public static void output(String output) {
    String insult1 = tpiCore.core[(new Random()).nextInt(insults.length)];
}

You can probably see from here what I'm trying to do. Pick a random insult from the list up above. If I try and run the code, it throws an error at tpiCore.core[(new Random()).nextInt(insults.length)];, "saying that The type of the expression must be an array type but it resolved to String". Then, when I change the type to Array, it throws up all kinds of errors along the core class. I don't know what I'm doing wrong. Can anyone help?

3
  • 1. what is tpiCore? 2. what is that instruction block under the declaration of core supposed to do? Commented May 10, 2013 at 9:00
  • 1
    +1 for livening up my Friday. Commented May 10, 2013 at 9:01
  • String insult1 = tpiCore.core[(new Random()).nextInt(insults.length)]; looks very dangerous... Commented May 10, 2013 at 9:02

2 Answers 2

2

If you must use static variables, here is how to do it.

public class TpiCore {

    private static String[] insults = new String[15];
    static {
        insults[0] = "xxx";
        insults[1] = "yyy";
        insults[2] = "zzz";
        // etc...
    }

    public static void main(String[] args) {
        String insult1 = TpiCore.insults[new Random().nextInt(insults.length)];
        System.out.println(insult1);
    }
}

However I would suggest something more like this. Good luck.

public class TpiCore {

    private String[] insults = new String[15];

    public TpiCore() {
        insults[0] = "xxx";
        insults[1] = "yyy";
        insults[2] = "zzz";
        // etc...
    }

    private String randomInsult() {
        return insults[new Random().nextInt(insults.length)];
    }

    public static void main(String[] args) {
        TpiCore core = new TpiCore();
        String insult1 = core.randomInsult();
        System.out.println(insult1);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried what you have sugested, but for some reason I still get the error. Is the problem just that I'm using JTextArea.append(randomInsult());?
Can you show us the relevant code? You need to use append on an instance of a JTextArea object, not on the class itself.
0

You are using core as an array, but it's defined as String

private static String core;

so you can't do

tpiCore.core[...]

you should do something like

tpiCore.insults[...]

Comments

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.