Here you can find the source of getRandomElementOrCompound(String equation)
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. |
public static String getRandomElementOrCompound(String equation)
//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())); } }