Example usage for java.util StringTokenizer countTokens

List of usage examples for java.util StringTokenizer countTokens

Introduction

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

Prototype

public int countTokens() 

Source Link

Document

Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.

Usage

From source file:cooccurrence.emf.java

/**
 * Outer Method to read the file content and populate it in the cooccur
 * hashmap/*from w  w w.jav a 2 s.co m*/
 *
 * @param filePath
 * @param cooccur
 * @return
 */
private static long readFileContents(String filePath, HashMap<String, HashMap<String, Double>> cooccur) {
    long totalCount = 0;
    try {

        BufferedReader br = new BufferedReader(new FileReader(filePath));
        String line = "";
        while ((line = br.readLine()) != null) {
            StringTokenizer tok = new StringTokenizer(line, "\t");
            if (tok.countTokens() == 3) {
                String from = tok.nextToken();
                String to = tok.nextToken();
                Double count = Double.parseDouble(tok.nextToken());
                totalCount += count;
                addToMatrix(from, to, count, cooccur);

            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(pmi.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(pmi.class.getName()).log(Level.SEVERE, null, ex);
    }
    return totalCount;
}

From source file:IPAddress.java

/**
 * Check if the specified address is a valid numeric TCP/IP address and return as an integer value
 * /*from   ww  w. j  a va  2 s .co m*/
 * @param ipaddr String
 * @return int
 */
public final static int parseNumericAddress(String ipaddr) {

    //   Check if the string is valid

    if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
        return 0;

    //   Check the address string, should be n.n.n.n format

    StringTokenizer token = new StringTokenizer(ipaddr, ".");
    if (token.countTokens() != 4)
        return 0;

    int ipInt = 0;

    while (token.hasMoreTokens()) {

        //   Get the current token and convert to an integer value

        String ipNum = token.nextToken();

        try {

            //   Validate the current address part

            int ipVal = Integer.valueOf(ipNum).intValue();
            if (ipVal < 0 || ipVal > 255)
                return 0;

            //   Add to the integer address

            ipInt = (ipInt << 8) + ipVal;
        } catch (NumberFormatException ex) {
            return 0;
        }
    }

    //   Return the integer address

    return ipInt;
}

From source file:Main.java

public static String[] getTokens(String line, String delim) {
    if (line == null || line.equals("")) {
        return null;
    }//  w w  w . j  a v a2  s . c  o  m
    if (line.indexOf(delim) < 0) {

        if (delim.length() > 1 && !"\r\n".equals(delim)) {
            String regex = "^.*[" + delim + "].*";
            if (!line.matches(regex)) {
                return new String[] { line };
            }
        } else {
            return new String[] { line };
        }
    }

    StringTokenizer st = new StringTokenizer(line, delim);
    int length = st.countTokens();
    String[] tokens = new String[length];
    for (int i = 0; i < length; i++) {
        tokens[i] = st.nextToken();
    }
    return tokens;
}

From source file:net.ripe.rpki.commons.util.VersionedId.java

public static VersionedId parse(String s) {
    Validate.notNull(s, "string required");
    StringTokenizer tokenizer = new StringTokenizer(s, ":");
    int count = tokenizer.countTokens();
    Validate.isTrue(count == 1 || count == 2, "invalid number of tokens in versioned id");
    long id = Long.parseLong(tokenizer.nextToken());
    long version;
    if (tokenizer.hasMoreTokens()) {
        version = Long.parseLong(tokenizer.nextToken());
    } else {/*from   ww w.  j  a v a  2 s .c o  m*/
        version = 0;
    }
    return new VersionedId(id, version);
}

From source file:Main.java

/**
 * Searches for a node within a DOM document with a given node path expression.
 * Elements are separated by '.' characters.
 * Example: Foo.Bar.Poo/*w  w  w. ja v a  2s.co  m*/
 * @param doc DOM Document to search for a node.
 * @param pathExpression dot separated path expression
 * @return Node element found in the DOM document.
 */
public static Node findNodeByName(Document doc, String pathExpression) {
    final StringTokenizer tok = new StringTokenizer(pathExpression, ".");
    final int numToks = tok.countTokens();
    NodeList elements;
    if (numToks == 1) {
        elements = doc.getElementsByTagNameNS("*", pathExpression);
        return elements.item(0);
    }

    String element = pathExpression.substring(pathExpression.lastIndexOf('.') + 1);
    elements = doc.getElementsByTagNameNS("*", element);

    String attributeName = null;
    if (elements.getLength() == 0) {
        //No element found, but maybe we are searching for an attribute
        attributeName = element;

        //cut off attributeName and set element to next token and continue
        Node found = findNodeByName(doc,
                pathExpression.substring(0, pathExpression.length() - attributeName.length() - 1));

        if (found != null) {
            return found.getAttributes().getNamedItem(attributeName);
        } else {
            return null;
        }
    }

    StringBuffer pathName;
    Node parent;
    for (int j = 0; j < elements.getLength(); j++) {
        int cnt = numToks - 1;
        pathName = new StringBuffer(element);
        parent = elements.item(j).getParentNode();
        do {
            if (parent != null) {
                pathName.insert(0, '.');
                pathName.insert(0, parent.getLocalName());//getNodeName());

                parent = parent.getParentNode();
            }
        } while (parent != null && --cnt > 0);
        if (pathName.toString().equals(pathExpression)) {
            return elements.item(j);
        }
    }

    return null;
}

From source file:Main.java

/**
 * Checks if Element Node is same as a Element name String
 *//*from   ww w. j  a v a2  s.c o  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:jnode.net.VMInetAddress.java

/**
 * Test if the hostname is an IP address and if so returns the address.
 * @param hostname/*from  w  ww .j a  va 2 s.c o m*/
 * @return The ip address, or null if it is not an IP address.
 */
private static byte[] getHostnameAsAddress(String hostname) {
    final StringTokenizer tok = new StringTokenizer(hostname, ".");
    if (tok.countTokens() != 4) {
        return null;
    }
    try {
        final byte b1 = parseUnsignedByte(tok.nextToken());
        final byte b2 = parseUnsignedByte(tok.nextToken());
        final byte b3 = parseUnsignedByte(tok.nextToken());
        final byte b4 = parseUnsignedByte(tok.nextToken());
        return new byte[] { b1, b2, b3, b4 };
    } catch (NumberFormatException ex) {
        return null;
    }
}

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:it.unipd.dei.ims.falcon.CmdLine.java

private static int[] parseIntArray(String s) {
    StringTokenizer t = new StringTokenizer(s, ",");
    int[] ia = new int[t.countTokens()];
    int ti = 0;//from   w  w  w.  ja v a2s . c  o m
    while (t.hasMoreTokens())
        ia[ti] = Integer.parseInt(t.nextToken());
    return ia;
}

From source file:com.alvermont.terraj.fracplanet.io.ColourFile.java

private static FloatRGBA parseColour(String line) throws IOException {
    final StringTokenizer tok = new StringTokenizer(line, ",");

    if (tok.countTokens() != 3) {
        throw new IOException("Line should be in the form x,y,z: " + line);
    }//from  w  ww . j  a va2 s.c om

    final float[] floats = new float[3];

    for (int b = 0; b < 3; ++b)
        floats[b] = Float.parseFloat(tok.nextToken().trim());

    return new FloatRGBA(floats[0], floats[1], floats[2]);
}