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: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);
    {//  w ww  .jav a2  s  .  c om
        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:keel.Algorithms.Genetic_Rule_Learning.Falco_GP.Main.java

/**
 * <p>//from   www .  ja va 2 s  . c om
 * Configure the execution of the algorithm.
 * 
 * @param jobFilename Name of the KEEL file with properties of the
 *                    execution
 *  </p>                  
 */

private static void configureJob(String jobFilename) {

    Properties props = new Properties();

    try {
        InputStream paramsFile = new FileInputStream(jobFilename);
        props.load(paramsFile);
        paramsFile.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(0);
    }

    // Files training and test
    String trainFile;
    String testFile;
    StringTokenizer tokenizer = new StringTokenizer(props.getProperty("inputData"));
    tokenizer.nextToken();
    trainFile = tokenizer.nextToken();
    trainFile = trainFile.substring(1, trainFile.length() - 1);
    testFile = tokenizer.nextToken();
    testFile = testFile.substring(1, testFile.length() - 1);

    tokenizer = new StringTokenizer(props.getProperty("outputData"));
    String reportTrainFile = tokenizer.nextToken();
    reportTrainFile = reportTrainFile.substring(1, reportTrainFile.length() - 1);
    String reportTestFile = tokenizer.nextToken();
    reportTestFile = reportTestFile.substring(1, reportTestFile.length() - 1);
    String reportRulesFile = tokenizer.nextToken();
    reportRulesFile = reportRulesFile.substring(1, reportRulesFile.length() - 1);

    // Algorithm auxiliar configuration
    XMLConfiguration algConf = new XMLConfiguration();
    algConf.setRootElementName("experiment");
    algConf.addProperty("process[@algorithm-type]",
            "net.sourceforge.jclec.problem.classification.falco.FalcoAlgorithm");
    algConf.addProperty("process.rand-gen-factory[@type]", "net.sourceforge.jclec.util.random.RanecuFactory");
    algConf.addProperty("process.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed")));
    algConf.addProperty("process.population-size", Integer.parseInt(props.getProperty("population-size")));
    algConf.addProperty("process.max-of-generations", Integer.parseInt(props.getProperty("max-generations")));
    algConf.addProperty("process.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size")));
    algConf.addProperty("process.dataset[@type]", "net.sourceforge.jclec.util.dataset.KeelDataSet");
    algConf.addProperty("process.dataset.train-data.file-name", trainFile);
    algConf.addProperty("process.dataset.test-data.file-name", testFile);
    algConf.addProperty("process.species[@type]",
            "net.sourceforge.jclec.problem.classification.falco.FalcoSyntaxTreeSpecies");
    algConf.addProperty("process.evaluator[@type]",
            "net.sourceforge.jclec.problem.classification.falco.FalcoEvaluator");
    algConf.addProperty("process.evaluator.alpha", Double.parseDouble(props.getProperty("alpha")));
    algConf.addProperty("process.provider[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeCreator");
    algConf.addProperty("process.parents-selector[@type]", "net.sourceforge.jclec.selector.RouletteSelector");
    algConf.addProperty("process.recombinator[@type]",
            "net.sourceforge.jclec.syntaxtree.SyntaxTreeRecombinator");
    algConf.addProperty("process.recombinator[@rec-prob]", Double.parseDouble(props.getProperty("rec-prob")));
    algConf.addProperty("process.recombinator.base-op[@type]",
            "net.sourceforge.jclec.problem.classification.falco.FalcoCrossover");
    algConf.addProperty("process.mutator[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeMutator");
    algConf.addProperty("process.mutator[@mut-prob]", Double.parseDouble(props.getProperty("mut-prob")));
    algConf.addProperty("process.mutator.base-op[@type]",
            "net.sourceforge.jclec.problem.classification.falco.FalcoMutator");
    algConf.addProperty("process.copy-prob", Double.parseDouble(props.getProperty("copy-prob")));
    algConf.addProperty("process.listener[@type]",
            "net.sourceforge.jclec.problem.classification.falco.KeelFalcoPopulationReport");
    algConf.addProperty("process.listener.report-dir-name", "./");
    algConf.addProperty("process.listener.train-report-file", reportTrainFile);
    algConf.addProperty("process.listener.test-report-file", reportTestFile);
    algConf.addProperty("process.listener.rules-report-file", reportRulesFile);
    algConf.addProperty("process.listener.global-report-name", "resumen");
    algConf.addProperty("process.listener.report-frequency", 50);

    try {
        algConf.save(new File("configure.txt"));
    } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    net.sourceforge.jclec.RunExperiment.main(new String[] { "configure.txt" });
}

From source file:keel.Algorithms.Genetic_Rule_Learning.Tan_GP.Main.java

/**
 * <p>/* ww  w .java2  s . c  o m*/
 * Configure the execution of the algorithm.
 * 
 * @param jobFilename Name of the KEEL file with properties of the
 *                    execution
 *  </p>                  
 */

private static void configureJob(String jobFilename) {

    Properties props = new Properties();

    try {
        InputStream paramsFile = new FileInputStream(jobFilename);
        props.load(paramsFile);
        paramsFile.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(0);
    }

    // Files training and test
    String trainFile;
    String testFile;
    StringTokenizer tokenizer = new StringTokenizer(props.getProperty("inputData"));
    tokenizer.nextToken();
    trainFile = tokenizer.nextToken();
    trainFile = trainFile.substring(1, trainFile.length() - 1);
    testFile = tokenizer.nextToken();
    testFile = testFile.substring(1, testFile.length() - 1);

    tokenizer = new StringTokenizer(props.getProperty("outputData"));
    String reportTrainFile = tokenizer.nextToken();
    reportTrainFile = reportTrainFile.substring(1, reportTrainFile.length() - 1);
    String reportTestFile = tokenizer.nextToken();
    reportTestFile = reportTestFile.substring(1, reportTestFile.length() - 1);
    String reportRulesFile = tokenizer.nextToken();
    reportRulesFile = reportRulesFile.substring(1, reportRulesFile.length() - 1);

    // Algorithm auxiliar configuration
    XMLConfiguration algConf = new XMLConfiguration();
    algConf.setRootElementName("experiment");
    algConf.addProperty("process[@algorithm-type]",
            "net.sourceforge.jclec.problem.classification.tan.TanAlgorithm");
    algConf.addProperty("process.rand-gen-factory[@type]", "net.sourceforge.jclec.util.random.RanecuFactory");
    algConf.addProperty("process.rand-gen-factory[@seed]", Integer.parseInt(props.getProperty("seed")));
    algConf.addProperty("process.population-size", Integer.parseInt(props.getProperty("population-size")));
    algConf.addProperty("process.max-of-generations", Integer.parseInt(props.getProperty("max-generations")));
    algConf.addProperty("process.max-deriv-size", Integer.parseInt(props.getProperty("max-deriv-size")));
    algConf.addProperty("process.dataset[@type]", "net.sourceforge.jclec.util.dataset.KeelDataSet");
    algConf.addProperty("process.dataset.train-data.file-name", trainFile);
    algConf.addProperty("process.dataset.test-data.file-name", testFile);
    algConf.addProperty("process.species[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanSyntaxTreeSpecies");
    algConf.addProperty("process.evaluator[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanEvaluator");
    algConf.addProperty("process.evaluator.w1", Double.parseDouble(props.getProperty("w1")));
    algConf.addProperty("process.evaluator.w2", Double.parseDouble(props.getProperty("w2")));
    algConf.addProperty("process.provider[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeCreator");
    algConf.addProperty("process.parents-selector[@type]", "net.sourceforge.jclec.selector.RouletteSelector");
    algConf.addProperty("process.recombinator[@type]",
            "net.sourceforge.jclec.syntaxtree.SyntaxTreeRecombinator");
    algConf.addProperty("process.recombinator[@rec-prob]", Double.parseDouble(props.getProperty("rec-prob")));
    algConf.addProperty("process.recombinator.base-op[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanCrossover");
    algConf.addProperty("process.mutator[@type]", "net.sourceforge.jclec.syntaxtree.SyntaxTreeMutator");
    algConf.addProperty("process.mutator[@mut-prob]", Double.parseDouble(props.getProperty("mut-prob")));
    algConf.addProperty("process.mutator.base-op[@type]",
            "net.sourceforge.jclec.problem.classification.tan.TanMutator");
    algConf.addProperty("process.copy-prob", Double.parseDouble(props.getProperty("copy-prob")));
    algConf.addProperty("process.elitist-prob", Double.parseDouble(props.getProperty("elitist-prob")));
    algConf.addProperty("process.support", Double.parseDouble(props.getProperty("support")));
    algConf.addProperty("process.listener[@type]",
            "net.sourceforge.jclec.problem.classification.tan.KeelTanPopulationReport");
    algConf.addProperty("process.listener.report-dir-name", "./");
    algConf.addProperty("process.listener.train-report-file", reportTrainFile);
    algConf.addProperty("process.listener.test-report-file", reportTestFile);
    algConf.addProperty("process.listener.rules-report-file", reportRulesFile);
    algConf.addProperty("process.listener.global-report-name", "resumen");
    algConf.addProperty("process.listener.report-frequency", 50);

    try {
        algConf.save(new File("configure.txt"));
    } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    net.sourceforge.jclec.RunExperiment.main(new String[] { "configure.txt" });
}

From source file:de.lgblaumeiser.ptm.cli.engine.CommandInterpreter.java

private StringTokenizer createTokens(final String command) {
    return new StringTokenizer(command);
}

From source file:correospingtelnet.Telnet.java

public static void main(String[] args) throws Exception {
    FileOutputStream fout = null;

    String remoteip = "64.62.142.154";

    int remoteport = 23;

    try {/*w  w w  .j  ava 2  s .  com*/
        fout = new FileOutputStream("spy.log", true);
    } catch (IOException 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 Telnet());
            tc.registerNotifHandler(new Telnet());
            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");
            System.out.println("Type ^[A-Z] to send the control character; use ^^ to send ^");

            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) {
                        final String line = new String(buff, 0, ret_read); // deliberate use of default charset
                        if (line.startsWith("AYT")) {
                            try {
                                System.out.println("Sending AYT");

                                System.out.println("AYT response:" + tc.sendAYT(5000));
                            } catch (IOException e) {
                                System.err.println("Exception waiting AYT response: " + e.getMessage());
                            }
                        } else if (line.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 (line.startsWith("REGISTER")) {
                            StringTokenizer st = new StringTokenizer(new String(buff));
                            try {
                                st.nextToken();
                                int opcode = Integer.parseInt(st.nextToken());
                                boolean initlocal = Boolean.parseBoolean(st.nextToken());
                                boolean initremote = Boolean.parseBoolean(st.nextToken());
                                boolean acceptlocal = Boolean.parseBoolean(st.nextToken());
                                boolean acceptremote = Boolean.parseBoolean(st.nextToken());
                                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 (line.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 (line.startsWith("SPY")) {
                            tc.registerSpyStream(fout);
                        } else if (line.startsWith("UNSPY")) {
                            tc.stopSpyStream();
                        } else if (line.matches("^\\^[A-Z^]\\r?\\n?$")) {
                            byte toSend = buff[1];
                            if (toSend == '^') {
                                outstr.write(toSend);
                            } else {
                                outstr.write(toSend - 'A' + 1);
                            }
                            outstr.flush();
                        } else {
                            try {
                                outstr.write(buff, 0, ret_read);
                                outstr.flush();
                            } catch (IOException e) {
                                end_loop = true;
                            }
                        }
                    }
                } catch (IOException e) {
                    System.err.println("Exception while reading keyboard:" + e.getMessage());
                    end_loop = true;
                }
            } while ((ret_read > 0) && (end_loop == false));

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

From source file:henplus.AbstractCommand.java

/**
 * convenience method: returns the number of elements in this string, separated by whitespace.
 *///from  w w  w  .j  av a 2s .c  o  m
protected int argumentCount(final String command) {
    return new StringTokenizer(command).countTokens();
}

From source file:hepple.postag.Lexicon.java

/**
 * Constructor.//from  w  w  w .  j av  a 2s. c  o  m
 * @param lexiconURL an URL for the file containing the lexicon.
 * @param encoding the character encoding to use for reading the lexicon.
 */
public Lexicon(URL lexiconURL, String encoding) throws IOException {
    String line;
    BufferedReader lexiconReader = null;
    InputStream lexiconStream = null;

    try {
        lexiconStream = lexiconURL.openStream();

        if (encoding == null) {
            lexiconReader = new BomStrippingInputStreamReader(lexiconStream);
        } else {
            lexiconReader = new BomStrippingInputStreamReader(lexiconStream, encoding);
        }

        line = lexiconReader.readLine();
        String entry;
        List<String> categories;
        while (line != null) {
            StringTokenizer tokens = new StringTokenizer(line);
            entry = tokens.nextToken();
            categories = new ArrayList<String>();
            while (tokens.hasMoreTokens())
                categories.add(tokens.nextToken());
            put(entry, categories);

            line = lexiconReader.readLine();
        } //while(line != null)
    } finally {
        IOUtils.closeQuietly(lexiconReader);
        IOUtils.closeQuietly(lexiconStream);
    }
}

From source file:com.l2jfree.gameserver.handler.admincommands.AdminMenu.java

@Override
public boolean useAdminCommand(String command, L2Player activeChar) {
    StringTokenizer st = new StringTokenizer(command);
    st.nextToken();//from w  w  w  . ja va  2  s . c o  m

    if (command.equals("admin_char_manage"))
        showMainPage(activeChar);
    else if (command.startsWith("admin_teleport_character_to_menu")) {
        String[] data = command.split(" ");
        if (data.length == 5) {
            String playerName = data[1];
            L2Player player = L2World.getInstance().getPlayer(playerName);
            if (player != null)
                teleportCharacter(player, Integer.parseInt(data[2]), Integer.parseInt(data[3]),
                        Integer.parseInt(data[4]), activeChar, "Admin is teleporting you.");
        }
        showMainPage(activeChar);
    } else if (command.startsWith("admin_recall_char")) {
        try {
            String targetName = st.nextToken();
            L2Player player = L2World.getInstance().getPlayer(targetName);
            teleportCharacter(player, activeChar.getX(), activeChar.getY(), activeChar.getZ(), activeChar,
                    "Admin is teleporting you.");
        } catch (Exception e) {
        }
    } else if (command.startsWith("admin_recall_party")) {
        int x = activeChar.getX(), y = activeChar.getY(), z = activeChar.getZ();
        try {
            String targetName = st.nextToken();
            L2Player player = L2World.getInstance().getPlayer(targetName);
            if (player == null) {
                activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
                return true;
            }
            if (!player.isInParty()) {
                activeChar.sendMessage("Player is not in party.");
                teleportCharacter(player, x, y, z, activeChar, "Admin is teleporting you.");
                return true;
            }
            for (L2Player pm : player.getParty().getPartyMembers())
                teleportCharacter(pm, x, y, z, activeChar, "Your party is being teleported by an Admin.");
        } catch (Exception e) {
        }
    } else if (command.startsWith("admin_recall_clan")) {
        int x = activeChar.getX(), y = activeChar.getY(), z = activeChar.getZ();
        try {
            String targetName = st.nextToken();
            L2Player player = L2World.getInstance().getPlayer(targetName);
            if (player == null) {
                activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
                return true;
            }
            L2Clan clan = player.getClan();
            if (clan == null) {
                activeChar.sendMessage("Player is not in a clan.");
                teleportCharacter(player, x, y, z, activeChar, "Admin is teleporting you.");
                return true;
            }
            L2Player[] members = clan.getOnlineMembers(0);
            for (L2Player element : members)
                teleportCharacter(element, x, y, z, activeChar, "Your clan is being teleported by an Admin.");
        } catch (Exception e) {
        }
    } else if (command.startsWith("admin_goto_char_menu")) {
        try {
            String targetName = st.nextToken();
            L2Player player = L2World.getInstance().getPlayer(targetName);
            teleportToCharacter(activeChar, player);
        } catch (Exception e) {
            activeChar.sendMessage("Target not found.");
            showMainPage(activeChar);
        }
    } else if (command.equals("admin_kill_menu")) {
        handleKill(activeChar);
    } else if (command.startsWith("admin_kick_menu")) {
        if (st.hasMoreTokens()) {
            String player = st.nextToken();
            L2Player plyr = L2World.getInstance().getPlayer(player);
            if (plyr != null) {
                new Disconnection(plyr).defaultSequence(false);
                activeChar.sendMessage("You kicked " + plyr.getName() + " from the game.");
            } else
                activeChar.sendMessage("Player " + player + " was not found in the game.");
        }
        showMainPage(activeChar);
    } else if (command.startsWith("admin_ban_menu")) {
        if (st.hasMoreTokens()) {
            String player = st.nextToken();
            L2Player plyr = L2World.getInstance().getPlayer(player);
            if (plyr != null) {
                new Disconnection(plyr).defaultSequence(false);
            }
            setAccountAccessLevel(player, activeChar, -100);
        }
        showMainPage(activeChar);
    } else if (command.startsWith("admin_unban_menu")) {
        if (st.hasMoreTokens()) {
            String player = st.nextToken();
            setAccountAccessLevel(player, activeChar, 0);
        }
        showMainPage(activeChar);
    }
    return true;
}

From source file:TextLayoutJustify.java

public void drawString(Graphics g, String line, int wc, int lineWidth, int y) {
    if (lineWidth < (int) (d.width * .75)) {
        g.drawString(line, 0, y);//from w  w w.j  a v a2 s  .  com
    } else {
        int toFill = (int) ((d.width - lineWidth) / wc);
        int nudge = d.width - lineWidth - (toFill * wc);
        StringTokenizer st = new StringTokenizer(line);
        int x = 0;
        while (st.hasMoreTokens()) {
            String word = st.nextToken();
            g.drawString(word, x, y);
            if (nudge > 0) {
                x = x + fm.stringWidth(word) + space + toFill + 1;
                nudge--;
            } else {
                x = x + fm.stringWidth(word) + space + toFill;
            }
        }
    }
}

From source file:eionet.cr.web.util.JstlFunctions.java

/**
 * Parses the given string with a whitespace tokenizer and looks up the first token whose length exceeds <tt>cutAtLength</tt>.
 * If such a token is found, returns the given string's <code>substring(0, i + cutAtLength) + "..."</code>, where <code>i</code>
 * is the start index of the found token. If no tokens are found that exceed the length of <tt>cutAtLength</tt>, then this
 * method simply return the given string.
 *
 * @return//from w w w.java 2  s  . co m
 */
public static java.lang.String cutAtFirstLongToken(java.lang.String str, int cutAtLength) {

    if (str == null) {
        return "";
    }

    String firstLongToken = null;
    StringTokenizer st = new StringTokenizer(str);
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (token.length() > cutAtLength) {
            firstLongToken = token;
            break;
        }
    }

    if (firstLongToken != null) {
        int i = str.indexOf(firstLongToken);
        StringBuffer buf = new StringBuffer(str.substring(0, i + cutAtLength));
        return buf.append("...").toString();
    } else {
        return str;
    }
}