1
static String[] checklist = {"FOR", "AXE", "JAM", "JAB", "ZIP", "ARE", "YOU", "JUG", "JAW", "JOY"};
public static void main (String[] args)
{
    int letterRange=26;
    int numberRange=10;

    String  x;
    String letters = new String(Letters(letterRange));
    int numbers = Numbers(numberRange);

    System.out.println(" The randomly generated License Plate is: " +letters + "-" +numbers);
}

public static char[] Letters(int lRange)
{
    char[] letters = new char[3];
    Random r = new Random();
    boolean match = false;
    boolean resultOk = false;
    String result;
    //Generating Random strings (character array)
    for (int x=0; x<letters.length; x++)
    {
        letters[x] = (char)(r.nextInt(26) + 65);
    }

    //Logic for possibility exclusion
    while (true)
    {
        if (match == true)
        {
            for (int x=0; x<letters.length; x++)
            {
                letters[x] = (char)(r.nextInt(26) + 65);
            }
        }
        result = new String(letters);
        for (int i = 0; i < checklist.length; i++)
        {
            if (result == checklist[i])
            {
                match = true;
                break;
            }
            if ((i == checklist.length - 1) && (match == false))
            {
                resultOk = true;
                break;
            }
        }
        if (resultOk == true)
        {
            break;
        }
    }
    return letters;

}

public static int Numbers(int nRange)
{
    int result = 0;
    int[] numbers = new int[3];
    Random r = new Random();
    for (int x = 0; x < numbers.length; x++)
    {
        numbers[x] = r.nextInt(nRange);
    } 
    for (int i = numbers.length; i >= 1; i--)
    {
        result += numbers[i-1] * (10 * (numbers.length - i));
    }
    return result;
}  

Basically I am trying to create a random license plate with three capital letters(65, ASCII code) and with three numbers. When I run the program, I get an exception for static int z=Integer.parseInt(y);. So basically what I did was that I converted String arrays to String, and then String to int and then int to char and after that I did a while loop(saying if letters is not equal to b), then it should work.

Can you please help me? Also, am I supposed to have two methods for this? And I want to surround the license plate by a box to make it look good.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
  • Can you tell me, what this `Integer.parseInt(Arrays.toString(x))` should return? Or what is the integer of `FOR, AXE, JAM, ...`? – Tom Nov 08 '14 at 15:11
  • Leave the box for now, what is the expected output? Could you describe what you do on each line, because I see some serious confusion here. – Maarten Bodewes Nov 08 '14 at 15:12
  • @Monty The `Arrays.toString(x)` call creates the string `"[FOR, AXE, JAM, JAB, ZIP, ARE, YOU, JUG, JAW, JOY]"`. That doesn't look like anything resembling a number. You're asking `Integer.parseInt` to interpret it as a number and put it into `z`. It doesn't know what number you want, and neither do we. If you don't want to interpret that set of words as a number, don't call `Integer.parseInt` on it. – Boann Nov 08 '14 at 15:13
  • 1
    Monly, you didn't program the code sample? – Maarten Bodewes Nov 08 '14 at 15:14
  • @Monty Neither has the JVM, that is why you get the mentioned error. Rethink what `z` should be/contain and either tell us this or change your code. – Tom Nov 08 '14 at 15:14
  • Throw away x, y, z, b and letters and the (infinite) while loop – laune Nov 08 '14 at 15:14
  • Everyone this is the question, Write a program to generate the values for a license plate that consists of three capital letters , chosen at random, followed by a space and then three digits chosen at random. Create a list of 10 3-letter words that the random words will be compared with so that these 10 words are excluded from possibilities –  Nov 08 '14 at 15:16
  • You can compose strings from characters and compare strings, so trying to convert string to int is pointless. – laune Nov 08 '14 at 15:19
  • OK, keep x, drop y, z, b and letters. Then you'll see some output. Then, think about creating the string from three characters, and how to look up the string in x. – laune Nov 08 '14 at 15:20
  • @Monty: To speak in your language: Plz do ur homework urself. You know, you aint learn if u have others do it for u. And plz keep ur ears open and do as advised instead of arguing. Coz nobdy is going to do it for u. – Markus W Mahlberg Nov 08 '14 at 15:20
  • @Monty You shouldn't guess which types for `z` will avoid your error. Start to think about what `z` should be/contain. Is it the amount of entries of `x`? Is it the length of the concatenated Strings of array `x`? Or something else? What have you tried to achieve with this variable? If you have your answer to this question, then you can think about for a proper data type for `z`. – Tom Nov 08 '14 at 15:22
  • Monty, I said "compose a string from characters" and advised you to compare a string with another string which can be an array element. – laune Nov 08 '14 at 15:23
  • As I told you, now you see the output. Now add the code for blocking those 10 three letter strings. – laune Nov 08 '14 at 15:24
  • @Monty But you already know that you want a `3` letter long word. So why do you need the length of that array `x`? Your word cannot get that long. Btw: this `static char b = (char) y;` doesn't work. And the same question as for `z` ... was is the purpose of `b`? You're not using it anywhere. And please read this help page about formatting your code: http://stackoverflow.com/help/formatting. – Tom Nov 08 '14 at 16:02
  • then we have to make it a char, cuz idk how to explain but it goanna generate a random letter three times –  Nov 08 '14 at 16:08
  • @Tom i seriously have no idea cuz that's my first time doing array and we haven't learned array yet –  Nov 08 '14 at 16:09
  • @Tom is it this http://stackoverflow.com/questions/7853502/how-to-convert-parse-from-string-to-char-in-java, it's the one with 19 votes –  Nov 08 '14 at 16:14
  • @Monty Give a minute, I'll write you an "anwer" that will give you a guideline on how to achieve your goal. But it won't contain the whole code. – Tom Nov 08 '14 at 16:17
  • it's ok, as long as your guideline helps me –  Nov 08 '14 at 16:19
  • 1
    @owlstead, thanks but the i wouldn't get it without a guideline –  Nov 08 '14 at 16:27
  • cuz i don't wanna get involved with converting alot, especially when i have learned anything for array –  Nov 08 '14 at 16:28
  • For this you only have to understand the for loop, `array.length` and `array[i]`; retrieving elements where `i` is the zero based index within the array. – Maarten Bodewes Nov 08 '14 at 16:33

3 Answers3

3

OK, for this time only as you would probably learn more from a good code sample at this point in time than anything else. Please study it and then rewrite it yourself, your teacher won't believe you otherwise and you wouldn't have learned anything.

public class LicensePlate {
    static String[] INVALID_PLATE_LETTERS = { "FOR", "AXE", "JAM", "JAB", "ZIP", "ARE", "YOU",
            "JUG", "JAW", "JOY" };

    static String generateLetters(int amount) {
        String letters = "";
        int n = 'Z' - 'A' + 1;
        for (int i = 0; i < amount; i++) {
            char c = (char) ('A' + Math.random() * n);
            letters += c;
        }
        return letters;
    }

    static String generateDigits(int amount) {
        String digits = "";
        int n = '9' - '0' + 1;
        for (int i = 0; i < amount; i++) {
            char c = (char) ('0' + Math.random() * n);
            digits += c;
        }
        return digits;
    }

    static String generateLicensePlate() {
        String licensePlate;
        String letters;
        do {
            letters = generateLetters(3);
        } while (illegalWord(letters));

        String digits = generateDigits(3);

        licensePlate = letters + "-" + digits;
        return licensePlate;
    }

    private static boolean illegalWord(String letters) {
        for (int i = 0; i < INVALID_PLATE_LETTERS.length; i++) {
            if (letters.equals(INVALID_PLATE_LETTERS[i])) {
                return true;
            }
        }
        return false;
    }

    public static void main(String args[]) {

        System.out.println(generateLicensePlate());
    }
}
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • 1
    Besides that, I don't like any class that forbids `"JOY"`. – Maarten Bodewes Nov 08 '14 at 16:26
  • can't u make String generateNumbers easier, like this for (int k = 0; k < 3; k++) { digits = 65 + ((int)(26 * Math.random())); System.out.print((char)digits); –  Nov 08 '14 at 16:35
  • i can't do it cuz i have to present in front of the class and tell them how it works? how am i goanna explain them a new concept ARRAY –  Nov 08 '14 at 16:37
  • Owlstead, can u please tell me how i can make a box for this –  Nov 08 '14 at 16:54
  • @Monty Digits don't start at 65. That would be the letter `A` in ASCII. You've already used an array yourself in your question. If you don't want that, just check each and every string separately. As for the box, it's fun to alter your code for such things, but it doesn't mean anything if you have to be told how. – Maarten Bodewes Nov 08 '14 at 17:42
  • i need to make a box surrounding the generate license plate? –  Nov 08 '14 at 17:43
  • You already know how for/next loops work, and string concatenation using `+`. All you need in addition is String.length(). – Maarten Bodewes Nov 08 '14 at 17:46
  • To create a box you'll have to repeat a character x times, then print a line, then print the license plate code (possibly surrounded by other characters, then another line of repeated characters. To know the size of the plate, you need the size of the code on the plate. – Maarten Bodewes Nov 08 '14 at 17:50
  • can u go here http://stackoverflow.com/questions/26695951/procedure-method-text-surrounded this was my previous code(i didn't do it). is it something like that –  Nov 08 '14 at 17:57
  • can u please help me with this? –  Nov 08 '14 at 18:00
  • What's wrong with the answer given for that question? – Maarten Bodewes Nov 08 '14 at 18:02
  • nothing is wrong, i meant was can i do that accept just take out inputString and make it x? and i only need the second method for the box right not the first one and can u tell me what i have to take out from the second method –  Nov 08 '14 at 18:05
  • You can just copy that method (or leave it in the separate class) and then *call* `drawTextBox` using `'*'` and the return value of `generateLicensePlate`, instead of calling System.out.println(generateLicensePlate())` – Maarten Bodewes Nov 08 '14 at 18:10
  • Note: I would strongly recommend that you try some of these questions with somebody next to you. You're inquisitive enough, but you may need somebody explaining things step by step. A Q/A site is not very good at this. – Maarten Bodewes Nov 08 '14 at 18:12
  • can u look at teh code i created? and tell me what i can do for teh box method –  Nov 08 '14 at 18:21
  • You really should not edit the original question too much on StackOverflow. Please use the code template of Tom. This seems to be a completely different tack to the problem. You should at least use `String.equals` instead of `==`. The while loop can have a condition. I'll leave it to use to guess which variable could be used. I'm out now, I've got some things to do for my own... – Maarten Bodewes Nov 08 '14 at 18:43
1

Let us start with your array:

static String[] x = {"FOR", "AXE", "JAM", "JAB", "ZIP", "ARE", "YOU", "JUG", "JAW", "JOY"};

It contains the prohibited words. That is good and you don't need anything else than this. No need to create a variable z that contains the array length or a variable b that contains some kind of char from this array.

Now to the main method:

public static void main(String args []) {
    String word;
    do {
        word = generateWord();
    } while(!isAllowed(word)); //generate new words until you've found an allowed one

    // print the generated and allowed word
    System.out.print(" - ");
    // generate 3 digits and print them
    System.out.println();
}

The purpose of this method is to generate you license plate. The assignment mentioned prohibited words (your array x). That means you have to generate the first part of the license plate until you've found a word that is allowed. This task is done in the do/while loop. It generates a 3 character long word and tests if it is allowed. If not, then the loop does another iteration. If "he" found an allowed word, the loop will be exited and your word is stored in the variable word.

The method that generates a 3 character long word looks something like this:

/** Generated a 3 character long word and returns it. */
public static String generateWord() {
    String result = "";
    // generate the word by adding 3 random chars to string "result".
    // You can append a char by doing: result = result + randomChar;
    return result;
}

You already know how to generate such a word, but instead of printing it, you'll need to return that word, so it can be used in the main method. It shouldn't be hard for you to fill this method correctly. The comment contains the "way" to add a generated char to an existing String.

This method:

/**
 * Tests if the given "word" is allowed or not.
 * If the word is in the array "x", then it is not allowed.
 * @return true if the word is allowed
 */
public static boolean isAllowed(String word) {
    /* Create a loop for array "x" that takes every entry of that array
     * and tests if it is the same as "word". If this is the case, then
     * return "false". If the word is not in the array, then this word
     * is allowed. Return "true" in this case.
     */
}

should check if the argument word is part of the array x, which contains the prohibited words. This method needs a loop that will check every single entry of array x. You could either use a for loor:

for (int i = 0; i < x.length; i++) { }

or a for each loop:

for (String entry : x) { }

You can find a lot of information about both loop types and how to use it with an array.

If you have an entry of x, then you need to test if this enty is the same word as the given word string. The class String has a perfect method for this task. Read the JavaDoc and you will find it: JavaDoc of class String.

I hope this guideline helps you achieving your task and I hope you have learned something from it. Good luck :).

Tom
  • 16,842
  • 17
  • 45
  • 54
  • 1
    If you're new to array, then read this page: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html. It might help you to understand the concept of arrays. – Tom Nov 08 '14 at 16:41
0

This is how I built it for class, straight forward no ...confusion.

public class LicensePlate {
   public static void main(String[] args) {

    // Generate three random uppercase letters
    int letter1 = 65 + (int)(Math.random() * (90 - 65));
    int letter2 = 65 + (int)(Math.random() * (90 - 65));
    int letter3 = 65 + (int)(Math.random() * (90 - 65));    

    // Generate four random digits
    int number1 = (int)(Math.random() * 10);
    int number2 = (int)(Math.random() * 10);
    int number3 = (int)(Math.random() * 10);

    // Display number plate
    System.out.println("" + (char)(letter1) + ((char)(letter2)) + 
        ((char)(letter3)) + "-" + number1 + number2 + number3);
    }
}