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:Main.java

public static void main(String[] args) {

    StringTokenizer st = new StringTokenizer("tutorial from java2s.com");

    // counting tokens
    System.out.println("Total tokens : " + st.countTokens());

    // checking tokens
    while (st.hasMoreTokens()) {
        System.out.println("Next token : " + st.nextToken());
    }/*from ww  w. ja v  a 2 s .com*/
}

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:ReplacingStringTokenizer.java

public static void main(String[] args) {
    String input = "But I'm not dead yet! I feel happy!";
    StringTokenizer stoke = new StringTokenizer(input);
    while (stoke.hasMoreElements())
        System.out.println(stoke.nextToken());
    System.out.println(Arrays.asList(input.split(" ")));
}

From source file:StringBufferCommaList.java

public static void main(String[] args) {
    StringTokenizer st = new StringTokenizer("Alpha Bravo Charlie");
    StringBuffer sb = new StringBuffer();
    while (st.hasMoreElements()) {
        sb.append(st.nextToken());/*from   w w  w.j  ava 2  s  . c  o  m*/
        if (st.hasMoreElements()) {
            sb.append(", ");
        }
    }
    System.out.println(sb);
}

From source file:LogStrTok.java

public static void main(String argv[]) {

    StringTokenizer matcher = new StringTokenizer(logEntryLine);

    System.out.println("tokens = " + matcher.countTokens());
    // StringTokenizer CAN NOT count if you are changing the delimiter!
    // if (matcher.countTokens() != NUM_FIELDS) {
    //    System.err.println("Bad log entry (or bug in StringTokenizer?):");
    //    System.err.println(logEntryLine);
    // }/*from   www. jav  a2  s .  c  om*/

    System.out.println("Hostname: " + matcher.nextToken());
    // StringTokenizer makes you ask for tokens in order to skip them:
    matcher.nextToken(); // eat the "-"
    matcher.nextToken(); // again
    System.out.println("Date/Time: " + matcher.nextToken("]"));
    //matcher.nextToken(" "); // again
    System.out.println("Request: " + matcher.nextToken("\""));
    matcher.nextToken(" "); // again
    System.out.println("Response: " + matcher.nextToken());
    System.out.println("ByteCount: " + matcher.nextToken());
    System.out.println("Referer: " + matcher.nextToken("\""));
    matcher.nextToken(" "); // again
    System.out.println("User-Agent: " + matcher.nextToken("\""));
}

From source file:StringReverse.java

public static void main(String[] argv) {
    //+/*from   ww w .  java2s .  c o m*/
    String s = "Father Charles Goes Down And Ends Battle";

    // Put it in the stack frontwards
    Stack myStack = new Stack();
    StringTokenizer st = new StringTokenizer(s);
    while (st.hasMoreTokens())
        myStack.push(st.nextElement());

    // Print the stack backwards
    System.out.print('"' + s + '"' + " backwards by word is:\n\t\"");
    while (!myStack.empty()) {
        System.out.print(myStack.pop());
        System.out.print(' ');
    }
    System.out.println('"');
    //-
}

From source file:edu.clemson.cs.nestbed.server.tools.BuildTestbed.java

public static void main(String[] args) {
    try {//  w ww  .j  ava2 s .  co  m
        BasicConfigurator.configure();
        //loadProperties();

        if (args.length < 2) {
            System.out.println("Usage: BuildTestbed <testbedID> <inputfile>");
            System.exit(0);
        }

        int testbedID = Integer.parseInt(args[0]);
        String filename = args[1];
        Connection conn = null;
        Statement statement = null;
        MoteSqlAdapter adapter = new MoteSqlAdapter();
        Map<Integer, Mote> motes = adapter.readMotes();

        log.info(motes);

        String connStr = System.getProperty("nestbed.options.databaseConnectionString");
        log.info("connStr: " + connStr);

        conn = DriverManager.getConnection(connStr);
        statement = conn.createStatement();

        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));

        String line;
        while ((line = in.readLine()) != null) {
            StringTokenizer tokenizer = new StringTokenizer(line);
            int address = Integer.parseInt(tokenizer.nextToken());
            String serial = tokenizer.nextToken();
            int xLoc = Integer.parseInt(tokenizer.nextToken());
            int yLoc = Integer.parseInt(tokenizer.nextToken());

            log.info("Input Mote:\n" + "-----------\n" + "address:  " + address + "\n" + "serial:   " + serial
                    + "\n" + "xLoc:     " + xLoc + "\n" + "yLoc:     " + yLoc);

            for (Mote i : motes.values()) {
                if (i.getMoteSerialID().equals(serial)) {
                    String query = "INSERT INTO MoteTestbedAssignments" + "(testbedID, moteID, moteAddress,"
                            + " moteLocationX, moteLocationY) VALUES (" + testbedID + ", " + i.getID() + ", "
                            + address + ", " + xLoc + ", " + yLoc + ")";
                    log.info(query);
                    statement.executeUpdate(query);
                }
            }
        }
        conn.commit();
    } catch (Exception ex) {
        log.error("Exception in main", ex);
    }
}

From source file:edu.gslis.ts.RunQuery.java

public static void main(String[] args) {
    try {/* w ww .ja  va 2 s  .c  o m*/
        // Get the commandline options
        Options options = createOptions();
        CommandLineParser parser = new GnuParser();
        CommandLine cmd = parser.parse(options, args);

        String inputPath = cmd.getOptionValue("input");
        String eventsPath = cmd.getOptionValue("events");
        String stopPath = cmd.getOptionValue("stop");
        int queryId = Integer.valueOf(cmd.getOptionValue("query"));

        List<String> ids = FileUtils.readLines(new File(inputPath + File.separator + "ids.txt"));

        Stopper stopper = new Stopper(stopPath);
        Map<Integer, FeatureVector> queries = readEvents(eventsPath, stopper);

        FeatureVector query = queries.get(queryId);

        Pairtree ptree = new Pairtree();
        Bag<String> words = new HashBag<String>();

        for (String streamId : ids) {

            String ppath = ptree.mapToPPath(streamId.replace("-", ""));

            String inpath = inputPath + File.separator + ppath + File.separator + streamId + ".xz";
            //                System.out.println(inpath);
            File infile = new File(inpath);
            InputStream in = new XZInputStream(new FileInputStream(infile));

            TTransport inTransport = new TIOStreamTransport(new BufferedInputStream(in));
            TBinaryProtocol inProtocol = new TBinaryProtocol(inTransport);
            inTransport.open();
            final StreamItem item = new StreamItem();

            while (true) {
                try {
                    item.read(inProtocol);
                    //                        System.out.println("Read " + item.stream_id);

                } catch (TTransportException tte) {
                    // END_OF_FILE is used to indicate EOF and is not an exception.
                    if (tte.getType() != TTransportException.END_OF_FILE)
                        tte.printStackTrace();
                    break;
                }
            }

            // Do something with this document...
            String docText = item.getBody().getClean_visible();

            StringTokenizer itr = new StringTokenizer(docText);
            while (itr.hasMoreTokens()) {
                words.add(itr.nextToken());
            }

            inTransport.close();

        }

        for (String term : words.uniqueSet()) {
            System.out.println(term + ":" + words.getCount(term));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:examples.TelnetClientExample.java

/***
 * Main for the TelnetClientExample./*w  ww.ja  v a 2s. com*/
 ***/
public static void main(String[] args) throws IOException {
    FileOutputStream fout = null;

    if (args.length < 1) {
        System.err.println("Usage: TelnetClientExample1 <remote-ip> [<remote-port>]");
        System.exit(1);
    }

    String remoteip = args[0];

    int remoteport;

    if (args.length > 1) {
        remoteport = (new Integer(args[1])).intValue();
    } else {
        remoteport = 23;
    }

    try {
        fout = new FileOutputStream("spy.log", true);
    } catch (Exception e) {
        System.err.println("Exception while opening the spy file: " + e.getMessage());
    }

    tc = new TelnetClient();

    TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
    EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
    SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);

    try {
        tc.addOptionHandler(ttopt);
        tc.addOptionHandler(echoopt);
        tc.addOptionHandler(gaopt);
    } catch (InvalidTelnetOptionException e) {
        System.err.println("Error registering option handlers: " + e.getMessage());
    }

    while (true) {
        boolean end_loop = false;
        try {
            tc.connect(remoteip, remoteport);

            Thread reader = new Thread(new TelnetClientExample());
            tc.registerNotifHandler(new TelnetClientExample());
            System.out.println("TelnetClientExample");
            System.out.println("Type AYT to send an AYT telnet command");
            System.out.println("Type OPT to print a report of status of options (0-24)");
            System.out.println("Type REGISTER to register a new SimpleOptionHandler");
            System.out.println("Type UNREGISTER to unregister an OptionHandler");
            System.out.println("Type SPY to register the spy (connect to port 3333 to spy)");
            System.out.println("Type UNSPY to stop spying the connection");

            reader.start();
            OutputStream outstr = tc.getOutputStream();

            byte[] buff = new byte[1024];
            int ret_read = 0;

            do {
                try {
                    ret_read = System.in.read(buff);
                    if (ret_read > 0) {
                        if ((new String(buff, 0, ret_read)).startsWith("AYT")) {
                            try {
                                System.out.println("Sending AYT");

                                System.out.println("AYT response:" + tc.sendAYT(5000));
                            } catch (Exception e) {
                                System.err.println("Exception waiting AYT response: " + e.getMessage());
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("OPT")) {
                            System.out.println("Status of options:");
                            for (int ii = 0; ii < 25; ii++)
                                System.out.println("Local Option " + ii + ":" + tc.getLocalOptionState(ii)
                                        + " Remote Option " + ii + ":" + tc.getRemoteOptionState(ii));
                        } else if ((new String(buff, 0, ret_read)).startsWith("REGISTER")) {
                            StringTokenizer st = new StringTokenizer(new String(buff));
                            try {
                                st.nextToken();
                                int opcode = (new Integer(st.nextToken())).intValue();
                                boolean initlocal = (new Boolean(st.nextToken())).booleanValue();
                                boolean initremote = (new Boolean(st.nextToken())).booleanValue();
                                boolean acceptlocal = (new Boolean(st.nextToken())).booleanValue();
                                boolean acceptremote = (new Boolean(st.nextToken())).booleanValue();
                                SimpleOptionHandler opthand = new SimpleOptionHandler(opcode, initlocal,
                                        initremote, acceptlocal, acceptremote);
                                tc.addOptionHandler(opthand);
                            } catch (Exception e) {
                                if (e instanceof InvalidTelnetOptionException) {
                                    System.err.println("Error registering option: " + e.getMessage());
                                } else {
                                    System.err.println("Invalid REGISTER command.");
                                    System.err.println(
                                            "Use REGISTER optcode initlocal initremote acceptlocal acceptremote");
                                    System.err.println("(optcode is an integer.)");
                                    System.err.println(
                                            "(initlocal, initremote, acceptlocal, acceptremote are boolean)");
                                }
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("UNREGISTER")) {
                            StringTokenizer st = new StringTokenizer(new String(buff));
                            try {
                                st.nextToken();
                                int opcode = (new Integer(st.nextToken())).intValue();
                                tc.deleteOptionHandler(opcode);
                            } catch (Exception e) {
                                if (e instanceof InvalidTelnetOptionException) {
                                    System.err.println("Error unregistering option: " + e.getMessage());
                                } else {
                                    System.err.println("Invalid UNREGISTER command.");
                                    System.err.println("Use UNREGISTER optcode");
                                    System.err.println("(optcode is an integer)");
                                }
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("SPY")) {
                            try {
                                tc.registerSpyStream(fout);
                            } catch (Exception e) {
                                System.err.println("Error registering the spy");
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("UNSPY")) {
                            tc.stopSpyStream();
                        } else {
                            try {
                                outstr.write(buff, 0, ret_read);
                                outstr.flush();
                            } catch (Exception e) {
                                end_loop = true;
                            }
                        }
                    }
                } catch (Exception e) {
                    System.err.println("Exception while reading keyboard:" + e.getMessage());
                    end_loop = true;
                }
            } while ((ret_read > 0) && (end_loop == false));

            try {
                tc.disconnect();
            } catch (Exception e) {
                System.err.println("Exception while connecting:" + e.getMessage());
            }
        } catch (Exception e) {
            System.err.println("Exception while connecting:" + e.getMessage());
            System.exit(1);
        }
    }
}

From source file:ch.unizh.ini.jaer.projects.gesture.vlccontrol.TelnetClientExample.java

/***
 * Main for the TelnetClientExample.//from ww w  .  j a v  a  2  s  . co  m
 ***/
public static void main(String[] args) throws IOException {
    FileOutputStream fout = null;

    if (args.length < 1) {
        System.err.println("Usage: TelnetClientExample1 <remote-ip> [<remote-port>]");
        System.exit(1);
    }

    String remoteip = args[0];

    int remoteport;

    if (args.length > 1) {
        remoteport = (new Integer(args[1])).intValue();
    } else {
        remoteport = 23;
    }

    try {
        fout = new FileOutputStream("spy.log", true);
    } catch (Exception e) {
        System.err.println("Exception while opening the spy file: " + e.getMessage());
    }

    tc = new TelnetClient();

    TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
    EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
    SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);

    try {
        tc.addOptionHandler(ttopt);
        tc.addOptionHandler(echoopt);
        tc.addOptionHandler(gaopt);
    } catch (InvalidTelnetOptionException e) {
        System.err.println("Error registering option handlers: " + e.getMessage());
    }

    while (true) {
        boolean end_loop = false;
        try {
            tc.connect(remoteip, remoteport);

            Thread reader = new Thread(new TelnetClientExample());
            tc.registerNotifHandler(new TelnetClientExample());
            System.out.println("TelnetClientExample");
            System.out.println("Type AYT to send an AYT telnet command");
            System.out.println("Type OPT to print a report of status of options (0-24)");
            System.out.println("Type REGISTER to register a new SimpleOptionHandler");
            System.out.println("Type UNREGISTER to unregister an OptionHandler");
            System.out.println("Type SPY to register the spy (connect to port 3333 to spy)");
            System.out.println("Type UNSPY to stop spying the connection");

            reader.start();
            OutputStream outstr = tc.getOutputStream();

            byte[] buff = new byte[1024];
            int ret_read = 0;

            do {
                try {
                    ret_read = System.in.read(buff);
                    if (ret_read > 0) {
                        if ((new String(buff, 0, ret_read)).startsWith("AYT")) {
                            try {
                                System.out.println("Sending AYT");

                                System.out.println("AYT response:" + tc.sendAYT(5000));
                            } catch (Exception e) {
                                System.err.println("Exception waiting AYT response: " + e.getMessage());
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("OPT")) {
                            System.out.println("Status of options:");
                            for (int ii = 0; ii < 25; ii++) {
                                System.out.println("Local Option " + ii + ":" + tc.getLocalOptionState(ii)
                                        + " Remote Option " + ii + ":" + tc.getRemoteOptionState(ii));
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("REGISTER")) {
                            StringTokenizer st = new StringTokenizer(new String(buff));
                            try {
                                st.nextToken();
                                int opcode = (new Integer(st.nextToken())).intValue();
                                boolean initlocal = (new Boolean(st.nextToken())).booleanValue();
                                boolean initremote = (new Boolean(st.nextToken())).booleanValue();
                                boolean acceptlocal = (new Boolean(st.nextToken())).booleanValue();
                                boolean acceptremote = (new Boolean(st.nextToken())).booleanValue();
                                SimpleOptionHandler opthand = new SimpleOptionHandler(opcode, initlocal,
                                        initremote, acceptlocal, acceptremote);
                                tc.addOptionHandler(opthand);
                            } catch (Exception e) {
                                if (e instanceof InvalidTelnetOptionException) {
                                    System.err.println("Error registering option: " + e.getMessage());
                                } else {
                                    System.err.println("Invalid REGISTER command.");
                                    System.err.println(
                                            "Use REGISTER optcode initlocal initremote acceptlocal acceptremote");
                                    System.err.println("(optcode is an integer.)");
                                    System.err.println(
                                            "(initlocal, initremote, acceptlocal, acceptremote are boolean)");
                                }
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("UNREGISTER")) {
                            StringTokenizer st = new StringTokenizer(new String(buff));
                            try {
                                st.nextToken();
                                int opcode = (new Integer(st.nextToken())).intValue();
                                tc.deleteOptionHandler(opcode);
                            } catch (Exception e) {
                                if (e instanceof InvalidTelnetOptionException) {
                                    System.err.println("Error unregistering option: " + e.getMessage());
                                } else {
                                    System.err.println("Invalid UNREGISTER command.");
                                    System.err.println("Use UNREGISTER optcode");
                                    System.err.println("(optcode is an integer)");
                                }
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("SPY")) {
                            try {
                                tc.registerSpyStream(fout);
                            } catch (Exception e) {
                                System.err.println("Error registering the spy");
                            }
                        } else if ((new String(buff, 0, ret_read)).startsWith("UNSPY")) {
                            tc.stopSpyStream();
                        } else {
                            try {
                                outstr.write(buff, 0, ret_read);
                                outstr.flush();
                            } catch (Exception e) {
                                end_loop = true;
                            }
                        }
                    }
                } catch (Exception e) {
                    System.err.println("Exception while reading keyboard:" + e.getMessage());
                    end_loop = true;
                }
            } while ((ret_read > 0) && (end_loop == false));

            try {
                tc.disconnect();
            } catch (Exception e) {
                System.err.println("Exception while connecting:" + e.getMessage());
            }
        } catch (Exception e) {
            System.err.println("Exception while connecting:" + e.getMessage());
            System.exit(1);
        }
    }
}