View Single Post
Old 04-23-2014, 11:38 PM
1. Luftballon 1. Luftballon is offline
Join Date: June 24, 2008
Posts: 3,363
Pronoun: it
1. Luftballon is an unknown quantity at this point
Default Re: First attempts at Java.

Originally Posted by Majora View Post
I created a program in Python that creates a random Pesterchum handle (the format being wordWord) from two dictionaries: one consisting of potential first words in the handle, the second consisting of potential second words in the handle. Just to experiment, I figured I would try to write the same program in Java (moreover, my host recently changed its cPanel and I'm super disoriented with it, so I'm trying to write a program in Java to spare time digging around for files that probably aren't worth digging around for).

However, it seems to me like Java doesn't really like generating random words, and that its specialty lies in numbers. After quite a bit of Google-searching around, I've come up empty. I still have no idea how to do this, since most advice that would seem worthwhile are completely useless to me, seeing as I am a complete novice in Java. The most I've ever done is drawn a picture of spaghetti with this language. That's basically it.

So, to clarify, I want to create a program in Java that selects one random word from both dictionaries (I think they're called arrays in Java?), then smashes them together and gives me the final result. No spaces. Nothing fancy. Just one lower-cased word, followed by an upper-cased word, without anything in between them, like I showed you above.

Any help would be greatly appreciated.
conceptually, the structure you'd like to select from is not a dictionary, but a set—you don't care about the order of the elements or any values associated with them.[1] an array in this context refers to a collection type which contains a fixed number of element cells determined at allocation time with a defined order and a selection function from an integer less than its length to an element determined by the integer provided.[2]

you'd like to take two sets of words, make one "random" selection from each, and concatenate them.

the implementation may be: your sets of words are arrays of String, and you make your "random" selections by requesting two "random" numbers (from java.util.Random, likely), and then indexing into each of the arrays.

such an implementation looks something like this:

import java.util.Random;

public class Chum
    private static String[] fst = {
        "third" };

    private static String[] snd = {
        "Fifth" };

    public static void main(String[] args) {
        Random rnGen = new Random();

        int fstIx = rnGen.nextInt(fst.length);
        String fstWord = fst[fstIx];

        int sndIx = rnGen.nextInt(snd.length);
        String sndWord = snd[sndIx];

        System.out.println(fstWord + sndWord); } }
disclaimers: this isn't very tested; the code is rather exaggerated; and, uh, my coding conventions are admittedly a bit unusual.[3]

uh if this is not comprehensible, like, ask or something.

[1]: of course, a set of A is isomorphic to a dictionary of A by unit (or void, where bottom values are permitted), which is to say a dictionary where the only information associated with a key is its existence.

[2]: the reason that an array is this way is that arrays can be implemented by allocating a block of memory of fixed size, which can be indexed into by adding the position of the first element to an offset multiplied by the size of each element. this isn't relevant in this context, though, as java doesn't admit pointer arithmetic.

[3]: ... my preferred language is haskell, and it shows. tangentially, everyone should totally give haskell a try! it's, like, mind-opening and eyeblowing or something like that.
Reply With Quote