List of usage examples for java.util StringTokenizer nextToken
public String nextToken(String delim)
From source file:Main.java
public static void main(String[] args) { // with delimeter StringTokenizer st = new StringTokenizer("tutorial/from/java2s.com"); // checking next token System.out.println("Next token is : " + st.nextToken("/")); System.out.println("Next token is : " + st.nextToken("/")); System.out.println("Next token is : " + st.nextToken("/")); }
From source file:Main.java
public static void main(String[] args) { StringTokenizer st = new StringTokenizer("tutorial/from/java2s.com", "/"); // checking next token System.out.println("Next token is : " + st.nextToken("/")); System.out.println("Next token is : " + st.nextToken("/")); System.out.println("Next token is : " + st.nextToken("/")); }
From source file:eu.crisis_economics.utilities.EnumDistribution.java
public static <T extends Enum<T>> EnumDistribution<T> // Immutable create(Class<T> token, String sourceFile) throws IOException { if (token == null) throw new NullArgumentException(); if (!token.isEnum()) throw new IllegalArgumentException("EnumDistribution: " + token.getSimpleName() + " is not an enum."); if (token.getEnumConstants().length == 0) throw new IllegalArgumentException("EnumDistribution: " + token.getSimpleName() + " is an empty enum."); EnumDistribution<T> result = new EnumDistribution<T>(); result.values = token.getEnumConstants(); result.probabilities = new EnumMap<T, Double>(token); Map<String, T> converter = new HashMap<String, T>(); final int numberOfValues = result.values.length; int[] valueIndices = new int[numberOfValues]; double[] valueProbabilities = new double[numberOfValues]; BitSet valueIsComitted = new BitSet(numberOfValues); {/*from w ww. j a v a 2 s.co m*/ int counter = 0; for (T value : result.values) { valueIndices[counter] = counter++; result.probabilities.put(value, 0.); converter.put(value.name(), value); } } BufferedReader reader = new BufferedReader(new FileReader(sourceFile)); try { String newLine; while ((newLine = reader.readLine()) != null) { if (newLine.isEmpty()) continue; StringTokenizer tokenizer = new StringTokenizer(newLine); final String name = tokenizer.nextToken(" ,:\t"), pStr = tokenizer.nextToken(" ,:\t"); if (tokenizer.hasMoreTokens()) throw new ParseException( "EnumDistribution: " + newLine + " is not a valid entry in " + sourceFile + ".", 0); final double p = Double.parseDouble(pStr); if (p < 0. || p > 1.) throw new IOException(pStr + " is not a valid probability for the value " + name); result.probabilities.put(converter.get(name), p); final int ordinal = converter.get(name).ordinal(); if (valueIsComitted.get(ordinal)) throw new ParseException("The value " + name + " appears twice in " + sourceFile, 0); valueProbabilities[converter.get(name).ordinal()] = p; valueIsComitted.set(ordinal, true); } { // Check sum of probabilities double sum = 0.; for (double p : valueProbabilities) sum += p; if (Math.abs(sum - 1.) > 1e2 * Math.ulp(1.)) throw new IllegalStateException("EnumDistribution: parser has succeeded, but the resulting " + "probaility sum (value " + sum + ") is not equal to 1."); } } catch (Exception e) { throw new IOException(e.getMessage()); } finally { reader.close(); } result.dice = new EnumeratedIntegerDistribution(valueIndices, valueProbabilities); return result; }
From source file:marytts.server.http.MaryHttpServerUtils.java
/** * Convert HTTP request string into key-value pairs * @param httpString the query part of the http request url * @param performUrlDecode whether to URL-decode the keys and values * @return/*from w w w . ja v a 2s. c o m*/ */ public static Map<String, String> toKeyValuePairs(String httpString, boolean performUrlDecode) { if (httpString == null || httpString.length() == 0) { return null; } Map<String, String> keyValuePairs = new HashMap<String, String>(); StringTokenizer st = new StringTokenizer(httpString); String newToken = null; String param, val; int equalSignInd; while (st.hasMoreTokens() && (newToken = st.nextToken("&")) != null) { equalSignInd = newToken.indexOf("="); //Default values unless we have a param=value pair param = newToken; val = ""; // //We have either a "param=value" pair, or "param=" only if (equalSignInd > -1) { param = newToken.substring(0, equalSignInd); val = newToken.substring(equalSignInd + 1); } if (performUrlDecode) { param = StringUtils.urlDecode(param); val = StringUtils.urlDecode(val); } keyValuePairs.put(param, val); } return keyValuePairs; }
From source file:com.concursive.connect.web.modules.search.utils.SearchUtils.java
/** * Description of the Method/*from www .j a va 2 s. c om*/ * * @param searchText Description of the Parameter * @return Description of the Return Value */ public static ArrayList<String> parseSearchTerms(String searchText) { ArrayList<String> terms = new ArrayList<String>(); StringBuffer sb = new StringBuffer(); boolean returnTokens = true; String currentDelims = WHITESPACE_AND_QUOTES; StringTokenizer parser = new StringTokenizer(searchText, currentDelims, returnTokens); String token = null; while (parser.hasMoreTokens()) { token = parser.nextToken(currentDelims); if (!isDoubleQuote(token)) { if (hasText(token)) { String gotToken = token.trim().toLowerCase(); if ("and".equals(gotToken) || "or".equals(gotToken) || "not".equals(gotToken)) { } else { if (sb.length() > 0) { sb.append(" "); } sb.append(gotToken); terms.add(sb.toString()); sb.setLength(0); } } } else { currentDelims = flipDelimiters(currentDelims); } } return terms; }
From source file:com.concursive.connect.web.modules.search.utils.SearchUtils.java
/** * Extracts the keywords into tokens, and then either concats them with AND * if all words are required, or leaves the tokens alone * * @param searchText Description of the Parameter * @param allWords Description of the Parameter * @return Description of the Return Value *///from ww w .jav a 2 s .c om public static String parseSearchText(String searchText, boolean allWords) { StringBuffer sb = new StringBuffer(); boolean returnTokens = true; String currentDelims = WHITESPACE_AND_QUOTES; StringTokenizer parser = new StringTokenizer(searchText, currentDelims, returnTokens); String token = null; boolean spacer = false; while (parser.hasMoreTokens()) { token = parser.nextToken(currentDelims); if (!isDoubleQuote(token)) { if (hasText(token)) { String gotToken = token.trim().toLowerCase(); if ("and".equals(gotToken) || "or".equals(gotToken) || "not".equals(gotToken)) { if (sb.length() > 0) { sb.append(" "); } sb.append(gotToken.toUpperCase()); spacer = true; } else { if (spacer) { if (sb.length() > 0) { sb.append(" "); } spacer = false; } else { if (sb.length() > 0) { if (allWords) { sb.append(" AND "); } else { sb.append(" "); } } } if (gotToken.indexOf(" ") > -1) { sb.append("\"").append(gotToken).append("\""); } else { sb.append(gotToken); } } } } else { currentDelims = flipDelimiters(currentDelims); } } return sb.toString(); }
From source file:gov.nih.nci.caIMAGE.util.NewDropdownUtil.java
static private List readListFromFile(String inFilename) throws Exception { List theReturnList = new ArrayList<Object>(); log.debug("Filename to read dropdown from: " + inFilename); BufferedReader in = new BufferedReader(new FileReader(inFilename)); boolean isDropdownOption = false; String str;//from w ww .jav a 2s . c o m while ((str = in.readLine()) != null) { log.debug("readListFromFile method: Reading value from file: " + str); // It's a DropdownOption file if (str.indexOf("DROPDOWN_OPTION") > 0) { isDropdownOption = true; } else if (isDropdownOption == true) { StringTokenizer theTokenizer = new StringTokenizer(str); String theLabel = theTokenizer.nextToken(","); String theValue = theTokenizer.nextToken(","); DropdownOption theDropdownOption = new DropdownOption(theLabel, theValue); theReturnList.add(theDropdownOption); } else { theReturnList.add(str); } } in.close(); return theReturnList; }