Example usage for java.lang Integer parseInt

List of usage examples for java.lang Integer parseInt

Introduction

In this page you can find the example usage for java.lang Integer parseInt.

Prototype

public static int parseInt(String s) throws NumberFormatException 

Source Link

Document

Parses the string argument as a signed decimal integer.

Usage

From source file:Main.java

public static void main(String[] args) {
    String s = "(123, 234; 345, 456) (567, 788; 899, 900)";
    Matcher m = Pattern.compile("\\d+").matcher(s);
    List<Integer> numbers = new ArrayList<Integer>();
    while (m.find()) {
        numbers.add(Integer.parseInt(m.group()));
    }/*  w ww .  j a va 2  s  . c  o m*/
    System.out.println(numbers);
}

From source file:AddException.java

/** Main program */
public static void main(String[] argv) {
    int number = 0;

    System.out.println("The number of words in argv is " + argv.length);

    if (argv.length == 0) {
        number = 1234;//  w  ww .j a v  a 2s. c  om
    } else if (argv.length == 1) {
        try {
            number = Integer.parseInt(argv[0]);
        } catch (NumberFormatException e) {
            System.err.println("Number " + argv[0] + " invalid (" + e.getMessage() + ").");
            System.exit(1);
        }
    } else {
        System.err.println("usage: UseArgv number");
        System.exit(1);
    }

    System.out.println("OK, number is " + number);
}

From source file:ParseDemo.java

public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;// ww w.j a va2  s .c  o m
    int i;
    int sum = 0;

    System.out.println("Enter numbers, 0 to quit.");
    do {
        str = br.readLine();
        try {
            i = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            System.out.println("Invalid format");
            i = 0;
        }
        sum += i;
        System.out.println("Current sum is: " + sum);
    } while (i != 0);
}

From source file:FibonacciTest.java

public static void main(String[] args) {

    ArrayBlockingQueue<Integer> queue;
    queue = new ArrayBlockingQueue<Integer>(10);
    new FibonacciProducer(queue);

    int nThreads = Integer.parseInt(args[0]);
    for (int i = 0; i < nThreads; i++)
        new FibonacciConsumer(queue);
}

From source file:com.core.TestService.java

public static void main(String args[]) {
    System.out.println(Integer.parseInt("043843"));
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b\\d+\\b";
    StringBuffer sb = new StringBuffer();
    String replacementText = "";
    String matchedText = "";

    String text = "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle.";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);//from   w  ww .  j a v a  2  s  .  co  m

    while (m.find()) {
        matchedText = m.group();
        int num = Integer.parseInt(matchedText);
        if (num == 1) {
            replacementText = "only one";
        } else if (num < 5) {
            replacementText = "a few";
        } else {
            replacementText = "many";
        }
        m.appendReplacement(sb, replacementText);
    }

    m.appendTail(sb);
    System.out.println("Old  Text: " + text);
    System.out.println("New Text: " + sb.toString());
}

From source file:AtomicTest.java

public static void main(String[] args) {

    nLoops = 10000;/*from  w w w .j  a  v a 2s  .  com*/
    nThreads = 1;
    doTest(new AtomicRunnable());
    doTest(new SyncRunnable());
    nLoops = Integer.parseInt(args[0]);
    nThreads = Integer.parseInt(args[1]);

    System.out.println("Starting atomic test");
    cleanGC();
    Timestamp atomicTS = new Timestamp();
    doTest(new AtomicRunnable());
    atomicTS.stop();
    System.out.println("Atomic took " + atomicTS);

    System.out.println("Starting sync test");
    cleanGC();
    Timestamp syncTS = new Timestamp();
    doTest(new SyncRunnable());
    syncTS.stop();
    System.out.println("Local sync took " + syncTS);

    double d = ((double) (syncTS.elapsedTime() - atomicTS.elapsedTime())) / (nLoops * nThreads);
    System.out.println("Atomic operation saves " + d + " " + syncTS.units() + " per call");
}

From source file:CollectionTest.java

public static void main(String[] args) {

    nLoops = 10000;//from   w  ww .  j av a 2 s  .  c om
    doTest(new Vector());
    doTest(new ArrayList());
    doTest(Collections.synchronizedList(new ArrayList()));
    nLoops = Integer.parseInt(args[0]);

    System.out.println("Starting synchronized vector test");
    cleanGC();
    Timestamp syncTS = new Timestamp();
    doTest(new Vector());
    syncTS.stop();
    System.out.println("Synchronized vector took " + syncTS);

    System.out.println("Starting unsynchronized vector test");
    cleanGC();
    Timestamp unsyncTS = new Timestamp();
    unsyncTS.stop();
    System.out.println("Unsynchronized vector took " + unsyncTS);

    double d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime())) / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " " + syncTS.units() + " per call");

    System.out.println("Starting synchronized array list test");
    cleanGC();
    syncTS = new Timestamp();
    doTest(Collections.synchronizedList(new ArrayList()));
    syncTS.stop();
    System.out.println("Synchronized array list took " + syncTS);

    System.out.println("Starting unsynchronized array list test");
    cleanGC();
    unsyncTS = new Timestamp();
    doTest(new ArrayList());
    unsyncTS.stop();
    System.out.println("Unsynchronized aray list took " + unsyncTS);

    d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime())) / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " " + syncTS.units() + " per call");
}

From source file:Main.java

public static void main(String args[]) {

    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent comp) {
            boolean returnValue;
            JTextField textField = (JTextField) comp;
            try {
                Integer.parseInt(textField.getText());
                returnValue = true;//  ww  w  .  j a v  a 2s  .  co m
            } catch (NumberFormatException e) {
                returnValue = false;
            }
            return returnValue;
        }
    };
    textField1.setInputVerifier(verifier);

    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.CENTER);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Socket sock = ssock.accept();
    ssock.close();/*w  ww .  j  av a 2s.co  m*/

    PrintStream pstream = new PrintStream(sock.getOutputStream());
    pstream.print("count? ");
    BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    String line = input.readLine();
    pstream.println("");
    int count = Integer.parseInt(line);
    for (int i = count; i >= 0; i--) {
        pstream.println(i);
    }
    pstream.close();
    sock.close();
}