Example usage for java.util StringTokenizer StringTokenizer

List of usage examples for java.util StringTokenizer StringTokenizer

Introduction

In this page you can find the example usage for java.util StringTokenizer StringTokenizer.

Prototype

public StringTokenizer(String str) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:WordCount.java

static void processLine(String line, Map map) {
    StringTokenizer st = new StringTokenizer(line);
    while (st.hasMoreTokens()) {
        addWord(map, st.nextToken());//  ww w.j a  v  a2s.c  o m
    }
}

From source file:Main.java

public static String[] toStringArray(String s) {
    StringTokenizer st = new StringTokenizer(s);
    String[] array = new String[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        array[i] = st.nextToken();/*from  ww  w. jav  a 2 s  .co m*/
    return array;
}

From source file:AnalyzeSentence.java

private static void analyze(String s) {
    System.out.println("\nnew sentence >> " + s);
    boolean sad = false;
    st = new StringTokenizer(s);
    while (st.hasMoreTokens()) {
        String token = next();//w  w w.  ja v  a2  s. c  om
        // Look until you find one of the
        // two starting tokens:
        if (!token.equals("I") && !token.equals("Are"))
            continue; // Top of while loop
        if (token.equals("I")) {
            String tk2 = next();
            if (!tk2.equals("am")) // Must be after I
                break; // Out of while loop
            else {
                String tk3 = next();
                if (tk3.equals("sad")) {
                    sad = true;
                    break; // Out of while loop
                }
                if (tk3.equals("not")) {
                    String tk4 = next();
                    if (tk4.equals("sad"))
                        break; // Leave sad false
                    if (tk4.equals("happy")) {
                        sad = true;
                        break;
                    }
                }
            }
        }
        if (token.equals("Are")) {
            String tk2 = next();
            if (!tk2.equals("you"))
                break; // Must be after Are
            String tk3 = next();
            if (tk3.equals("sad"))
                sad = true;
            break; // Out of while loop
        }
    }
    if (sad)
        System.out.println("Sad detected");
}

From source file:Main.java

public static int[] toIntArray(String s) {
    StringTokenizer st = new StringTokenizer(s);
    int[] array = new int[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        array[i] = Integer.parseInt(st.nextToken());
    return array;
}

From source file:Main.java

public static void killProcess(String packageName) {
    String processId = "";
    try {//from  w ww  .j  ava2 s .  co  m
        Runtime r = Runtime.getRuntime();
        Process p = r.exec("ps");
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inline;
        while ((inline = br.readLine()) != null) {
            if (packageName != null) {
                if (inline.contains(packageName)) {
                    break;
                }
            } else {
                Log.e("PvTorrent_proc", "packageName is null");
            }

        }
        br.close();
        Log.i("PvTorrent_proc", "inline" + inline);
        if (inline != null) {

            StringTokenizer processInfoTokenizer = new StringTokenizer(inline);
            int count = 0;
            while (processInfoTokenizer.hasMoreTokens()) {
                count++;
                processId = processInfoTokenizer.nextToken();
                if (count == 2) {
                    break;
                }
            }
            Log.i("PvTorrent_proc", "kill process : " + processId);
            r.exec("kill -9 " + processId);
        }
    } catch (IOException ex) {
        Log.e("PvTorrent_proc", "kill" + ex.getStackTrace());
    }
}

From source file:com.spotify.scio.extra.transforms.ProcessUtil.java

static String[] tokenizeCommand(String command) {
    StringTokenizer st = new StringTokenizer(command);
    String[] cmdArray = new String[st.countTokens()];
    for (int i = 0; st.hasMoreTokens(); i++)
        cmdArray[i] = st.nextToken();/*from  w  w  w .j  ava2  s.  c  o m*/
    return cmdArray;
}

From source file:Main.java

/**
 * Checks if Element Node is same as a Element name String
 *//* www  .j a  v a  2 s  .  co  m*/

public static boolean isStrElementNode(String elementName, Node elementNode, boolean ignoreCase)

{

    if ((elementNode == null) || (elementName == null) ||

            (elementName.trim().equals("")) || (elementNode.getNodeType() != Node.ELEMENT_NODE))

        return false;

    StringTokenizer tokenizer = new StringTokenizer(":");

    int numTokens = tokenizer.countTokens();

    if (numTokens == 1)

    {

        String name = (String) tokenizer.nextElement();

        Element element = (Element) elementNode;

        if (element.getNamespaceURI() != null)

            return false;

        if (ignoreCase)

            return element.getNodeName().trim().equalsIgnoreCase(elementName);

        return element.getNodeName().trim().equals(elementName);

    } else if (numTokens == 2)

    {

        String namespace = (String) tokenizer.nextElement();

        String localName = (String) tokenizer.nextElement();

        Element element = (Element) elementNode;

        if (element.getNamespaceURI() == null)

            return false;

        if (ignoreCase)

            return ((element.getLocalName().trim().equalsIgnoreCase(localName)) &&

                    (element.getNamespaceURI().equalsIgnoreCase(namespace.trim())));

        return ((element.getLocalName().trim().equals(localName)) &&

                (element.getNamespaceURI().equals(namespace.trim())));

    } else

        return false;

}

From source file:MainResourceBundle.java

@Override
public Enumeration getKeys() {
    StringTokenizer key = new StringTokenizer("Hello from java2s.com!");
    return key;
}

From source file:blue.noteProcessor.TempoMapper.java

public static TempoMapper createTempoMapper(String timeWarpString) {
    TempoMapper tm = new TempoMapper();
    StringTokenizer st = new StringTokenizer(timeWarpString);
    String time, tempo;/*  w w w.  j a  v  a2  s.  c om*/
    BeatTempoPair temp;

    if (st.countTokens() % 2 != 0) {
        // not an even amount of tokens!
        return null;
    }

    tm.timeMap = new BeatTempoPair[st.countTokens() / 2];
    int index = 0;

    BeatTempoPair[] tMap = tm.timeMap;

    while (st.hasMoreTokens()) {
        try {
            time = st.nextToken();
            tempo = st.nextToken();

            temp = new BeatTempoPair();
            temp.beat = Float.parseFloat(time);
            temp.tempo = Float.parseFloat(tempo);

            if (temp.beat < 0.0f || temp.tempo <= 0.0f) {
                return null;
            }

            tMap[index] = temp;

            if (index > 0) {

                float factor1 = 60.0f / tMap[index - 1].tempo;
                float factor2 = 60.0f / tMap[index].tempo;
                float deltaBeat = tMap[index].beat - tMap[index - 1].beat;

                float acceleration = 0.0f;

                if (deltaBeat >= 0.0f) {
                    acceleration = (factor2 - factor1) / (tMap[index].beat - tMap[index - 1].beat);
                }

                if (tMap[index].beat == tMap[index - 1].beat) {
                    tMap[index].accumulatedTime = tMap[index - 1].accumulatedTime;
                } else {
                    tMap[index].accumulatedTime = tMap[index - 1].accumulatedTime
                            + getAreaUnderCurve(factor1, deltaBeat, acceleration);
                }
            }

            index++;
        } catch (Exception e) {
            // if there's any errors whatsoever, return null
            // and let the calling procedure handle it
            return null;
        }
    }
    return tm;
}

From source file:edu.umd.cfar.lamp.viper.geometry.Circle.java

/**
 * Creates a new <code>Circle</code> from the string.
 * @param S a string in the form <em>x y radius</em>
 * @return new <code>Circle</code> from the string
 * @throws BadAttributeDataException upon a parse error
 *///from w ww.  j  a  va 2 s .  co m
public static Circle valueOf(String S) {
    try {
        StringTokenizer st = new StringTokenizer(S);
        return new Circle(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                Integer.parseInt(st.nextToken()));
    } catch (NoSuchElementException e1) {
        throw new BadAttributeDataException("Not enough integers for circle: " + S);
    } catch (NumberFormatException e2) {
        throw new BadAttributeDataException("Malformed circle string: " + S);
    }
}