Java Random String getRandomElementOrCompound(String equation)

Here you can find the source of getRandomElementOrCompound(String equation)

Description

Accessor method used to return a random element in the chemical equation you specify.

License

Open Source License

Parameter

Parameter Description
equation String reference: The chemical equation that you would like for a random element to be returned.
list ArrayList of Strings reference: Stores the list of possible verified elements that can be returned.
st StringTokenizer reference: Allows the use of the StringTokenizer to split a string by the spaces to determine the elements in the equation.
element String reference: Stores the next potential element to be processed.

Return

String reference: A random element in the chemical equation.

Declaration

public static String getRandomElementOrCompound(String equation) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//from  w ww  .  j a  v a 2s . c om
     * Accessor method used to return a random element in the chemical equation you specify.
     * While loop is used to indefinitely scan every token in the equation String until every possible token has been processed.
     * If structure is used to exclude tokens that are not elements, in this case the tokens can either be "+" or "->".
     * @return String reference: A random element in the chemical equation.
     * @param equation String reference: The chemical equation that you would like for a random element to be returned.
     * @param list ArrayList of Strings reference: Stores the list of possible verified elements that can be returned.
     * @param st StringTokenizer reference: Allows the use of the StringTokenizer to split a string by the spaces to determine the elements in the equation.
     * @param element String reference: Stores the next potential element to be processed.
     */
    public static String getRandomElementOrCompound(String equation) {
        ArrayList<String> list = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(equation);
        while (st.hasMoreTokens()) {
            String element = st.nextToken();
            if (!element.equals("+") && !element.equals("->")) {
                list.add(element);
            }
        }
        return list.get((int) (Math.random() * list.size()));
    }
}

Related

  1. genRandomString(final int length)
  2. genRandomString(int count)
  3. getDefaultRandomName(String namePrefix)
  4. getRandomAString(int min, int max)
  5. getRandomColors(final int size, final List colors)
  6. getRandomFromTier(int tier, String lvlRange)
  7. getRandomNumberString(int length)
  8. getRandomNumberString(int maxValue, int strLen)
  9. getRandomNumberStringBase36(int strLen)