Example usage for org.apache.commons.lang3.math NumberUtils toInt

List of usage examples for org.apache.commons.lang3.math NumberUtils toInt

Introduction

In this page you can find the example usage for org.apache.commons.lang3.math NumberUtils toInt.

Prototype

public static int toInt(final String str) 

Source Link

Document

Convert a String to an int, returning zero if the conversion fails.

If the string is null, zero is returned.

 NumberUtils.toInt(null) = 0 NumberUtils.toInt("")   = 0 NumberUtils.toInt("1")  = 1 

Usage

From source file:pcgen.system.PropertyContext.java

public int getInt(String key) {
    return NumberUtils.toInt(getProperty(key));
}

From source file:pcgen.system.PropertyContext.java

public int getInt(String key, int defaultValue) {
    return NumberUtils.toInt(getProperty(key, Integer.toString(defaultValue)));
}

From source file:pcgen.system.PropertyContext.java

public int initInt(String key, int defaultValue) {
    return NumberUtils.toInt(initProperty(key, Integer.toString(defaultValue)));
}

From source file:ruc.irm.wikit.db.Wikipedia.java

public static void main(String[] args) throws ParseException, IOException {
    String helpMsg = "usage: Wikipedia -c config.xml -pid 123";

    HelpFormatter helpFormatter = new HelpFormatter();
    CommandLineParser parser = new PosixParser();
    Options options = new Options();
    options.addOption(new Option("c", true, "config file"));
    options.addOption(new Option("type", true, "database type: " + "articleByTitle|categoryByTitle|page"));

    CommandLine commandLine = parser.parse(options, args);
    if (!commandLine.hasOption("c") || !commandLine.hasOption("type")) {
        helpFormatter.printHelp(helpMsg, options);
        return;/*from  w w w .  ja  va  2 s .  c o  m*/
    }

    Conf conf = ConfFactory.createConf(commandLine.getOptionValue("c"), true);
    Wikipedia wikipedia = new Wikipedia(conf);
    String type = commandLine.getOptionValue("type");

    switch (type) {
    case "page":
        ConsoleLoop.loop(new ImmutableTriple<String, String, ConsoleLoop.Handler>("id", "list page by id",
                new ConsoleLoop.Handler() {
                    @Override
                    public void handle(String input) throws IOException {
                        Page page = wikipedia.getPageById(NumberUtils.toInt(input));
                        System.out.println(page);
                        if (page != null) {
                            System.out.println(page.getContent());
                        }
                    }
                }), new ImmutableTriple<String, String, ConsoleLoop.Handler>("list", "list all",
                        new ConsoleLoop.Handler() {
                            @Override
                            public void handle(String input) throws IOException {
                                PageIterator it = wikipedia.getPageIterator();
                                Scanner scanner = new Scanner(System.in);
                                while (it.hasNext()) {
                                    Page page = it.next();
                                    System.out.println(page);
                                    if (page.getContent().length() > 200) {
                                        System.out.println(page.getContent().substring(0, 200));
                                    } else {
                                        System.out.println(page.getContent());
                                    }

                                    System.out.println("type exit to return, or enter to continue");
                                    String command = scanner.nextLine();
                                    if (command.equalsIgnoreCase("exit")) {
                                        break;
                                    }
                                }
                                it.close();
                            }
                        }));
        break;
    case "articleByTitle":
        ConsoleLoop.loop(new ImmutableTriple<String, String, ConsoleLoop.Handler>("title",
                "input title " + "and return its id", new ConsoleLoop.Handler() {
                    @Override
                    public void handle(String input) throws IOException {
                        Integer id = wikipedia.getIdByArticleTitle(input);
                        System.out.println(id);
                    }
                }), new ImmutableTriple<String, String, ConsoleLoop.Handler>("list",
                        "list all " + "article title and its id", new ConsoleLoop.Handler() {
                            @Override
                            public void handle(String input) throws IOException {
                                WIterator<String, Integer> it = wikipedia.getArticleTitleIterator();
                                int count = 0;
                                while (it.hasNext()) {
                                    WEntry<String, Integer> entry = it.next();
                                    System.out.println("\t" + entry.getKey() + "\t" + entry.getValue());

                                    count++;
                                    if (count % 20 == 0) {
                                        System.out.println("type exit to return, or enter to continue");
                                        String command = new Scanner(System.in).nextLine();
                                        if (command.equalsIgnoreCase("exit")) {
                                            break;
                                        }
                                    }
                                }
                                it.close();
                            }
                        }));
        break;
    case "categoryByTitle":
        ConsoleLoop.loop(new ImmutableTriple<String, String, ConsoleLoop.Handler>("title",
                "get id by " + "title", new ConsoleLoop.Handler() {
                    @Override
                    public void handle(String input) throws IOException {
                        Integer id = wikipedia.getIdByCategoryTitle(input);
                        System.out.println(id);
                    }
                }), new ImmutableTriple<String, String, ConsoleLoop.Handler>("list", "list all",
                        new ConsoleLoop.Handler() {
                            @Override
                            public void handle(String input) throws IOException {
                                WIterator<String, Integer> it = wikipedia.getCategoryTitleIterator();
                                int count = 0;
                                while (it.hasNext()) {
                                    WEntry<String, Integer> entry = it.next();
                                    System.out.println("\t" + entry.getKey() + "\t" + entry.getValue());

                                    count++;
                                    if (count % 20 == 0) {
                                        System.out.println("type exit to return, or enter to continue");
                                        String command = new Scanner(System.in).nextLine();
                                        if (command.equalsIgnoreCase("exit")) {
                                            break;
                                        }
                                    }
                                }
                                it.close();
                            }
                        }));
        break;
    }
    ;

    wikipedia.close();
}

From source file:uk.co.jwlawson.jcluster.demos.Mutator.java

/**
 * Check whether the array contains the right number of elements to define a matrix.
 */// w w w  .jav a  2  s.  co m
private static boolean isValidMatrix(String[] args) {
    int rows = NumberUtils.toInt(args[0]);
    int cols = NumberUtils.toInt(args[1]);
    return (rows * cols == args.length - 2);
}

From source file:uk.co.jwlawson.jcluster.demos.Mutator.java

/** Convert a string array to an int array. */
private int[] toIntArray(String[] arr) {
    int[] result = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        result[i] = NumberUtils.toInt(arr[i]);
    }//from  ww  w .j av  a 2s .  co  m
    return result;
}