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

public static void main(String[] args) {
    target = 10000;/*from  w  w  w  .j  av a  2s .c  om*/
    doTestCreate();
    doTestPool(8);
    doTestLoop();

    target = Integer.parseInt(args[0]);
    cleanGC();
    Timestamp createTS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestCreate();
    createTS.stop();
    System.out.println("Create thread test took " + createTS);

    cleanGC();
    Timestamp pool8TS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestPool(8);
    pool8TS.stop();
    System.out.println("Pool test (8 threads) took " + pool8TS);

    cleanGC();
    Timestamp poolTS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestPool(1);
    poolTS.stop();
    System.out.println("Pool test (1 thread) took " + poolTS);

    cleanGC();
    Timestamp loopTS = new Timestamp(TimeUnit.MICROSECONDS);
    doTestLoop();
    loopTS.stop();
    System.out.println("Loop test took " + loopTS);

    double d = ((double) (createTS.elapsedTime() - loopTS.elapsedTime())) / target;
    System.out.println("Creating a thread took " + d + " " + loopTS.units() + " per thread");

    d = ((double) (createTS.elapsedTime() - poolTS.elapsedTime())) / target;
    System.out.println("Using a thread pool (1 thread) saved  " + d + " " + loopTS.units() + " per task");

    d = ((double) (createTS.elapsedTime() - pool8TS.elapsedTime())) / target;
    System.out.println("Using a thread pool (8 threads) saved  " + d + " " + loopTS.units() + " per task");

    d = ((double) (poolTS.elapsedTime() - loopTS.elapsedTime())) / target;
    System.out.println("Thread pool overhead (1 thread) is " + d + " " + loopTS.units() + " per task");

    d = ((double) (pool8TS.elapsedTime() - loopTS.elapsedTime())) / target;
    System.out.println("Thread pool overhead (8 threads) is " + d + " " + loopTS.units() + " per task");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    System.out.print("Enter your name: ");
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);
    String name = in.readLine();/*  w  ww. j  a v  a 2 s .  c o  m*/
    System.out.println("Hello, " + name + ". Enter three ints...");
    int[] values = new int[3];
    double sum = 0.0;
    for (int i = 0; i < values.length; i++) {
        System.out.print("Number " + (i + 1) + ": ");
        String temp = in.readLine();
        values[i] = Integer.parseInt(temp);
        sum += values[i];
    }
    System.out.println("The average equals " + sum / values.length);
}

From source file:MulticastSniffer.java

public static void main(String[] args) {

    InetAddress ia = null;/*from w  w  w  .ja  va  2s .c  o  m*/
    byte[] buffer = new byte[65509];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    int port = 0;

    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            //
        }
        port = Integer.parseInt(args[1]);
    } // end try
    catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSniffer MulticastAddress port");
        System.exit(1);
    }

    try {
        MulticastSocket ms = new MulticastSocket(port);
        ms.joinGroup(ia);
        while (true) {
            ms.receive(dp);
            String s = new String(dp.getData(), 0, 0, dp.getLength());
            System.out.println(s);
        }
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }

}

From source file:UDPReceive.java

public static void main(String args[]) {
    try {// w  w w . ja v  a  2s  .c  o m
        if (args.length != 1)
            throw new IllegalArgumentException("Wrong number of args");

        // Get the port from the command line
        int port = Integer.parseInt(args[0]);

        // Create a socket to listen on the port.
        DatagramSocket dsocket = new DatagramSocket(port);

        // Create a buffer to read datagrams into. If anyone sends us a
        // packet containing more than will fit into this buffer, the
        // excess will simply be discarded!
        byte[] buffer = new byte[2048];

        // Create a packet to receive data into the buffer
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        // Now loop forever, waiting to receive packets and printing them.
        for (;;) {
            // Wait to receive a datagram
            dsocket.receive(packet);

            // Decode the bytes of the packet to characters, using the
            // UTF-8 encoding, and then display those characters.
            String msg = new String(buffer, 0, packet.getLength(), "UTF-8");
            System.out.println(packet.getAddress().getHostName() + ": " + msg);

            // Reset the length of the packet before reusing it.
            // Prior to Java 1.1, we'd just create a new packet each time.
            packet.setLength(buffer.length);
        }
    } catch (Exception e) {
        System.err.println(e);
        System.err.println(usage);
    }
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    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;/*from  w  w  w. j  a  va2s .  c o  m*/
            } catch (NumberFormatException e) {
                returnValue = false;
            }
            return returnValue;
        }
    };
    textField1.setInputVerifier(verifier);
    frame.add(textField1, BorderLayout.NORTH);
    frame.add(textField2, BorderLayout.CENTER);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:HolidaySked.java

public static void main(String[] arguments) {
    HolidaySked cal = new HolidaySked();
    if (arguments.length > 0) {
        try {/*from w ww.j  a v a  2s .com*/
            int whichDay = Integer.parseInt(arguments[0]);
            if (cal.isHoliday(whichDay)) {
                System.out.println("Day number " + whichDay + " is a holiday.");
            } else {
                System.out.println("Day number " + whichDay + " is not a holiday.");
            }
        } catch (NumberFormatException nfe) {
            System.out.println("Error: " + nfe.getMessage());
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    try {/*from w w w.  jav a  2 s .  c o  m*/
        System.out.print("Enter your name: ");
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(reader);

        String name = in.readLine();
        System.out.println("Hello, " + name + ". Enter three ints...");
        int[] values = new int[3];
        double sum = 0.0;

        for (int i = 0; i < values.length; i++) {
            System.out.print("Number " + (i + 1) + ": ");
            String temp = in.readLine();
            values[i] = Integer.parseInt(temp);
            sum += values[i];
        }

        System.out.println("The average equals " + sum / values.length);

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

From source file:AnotherBeerServer.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    System.out.println("Listening");
    Socket sock = ssock.accept();
    ssock.close(); // no more connects

    PrintStream ps = new PrintStream(sock.getOutputStream());

    // ask for count
    ps.print("count? ");
    BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));

    // read and parse it
    String line = input.readLine();
    ps.println("");
    int count = Integer.parseInt(line);
    for (int i = count; i >= 0; i--) {
        ps.println(i + " Java Source and Support.");
    }//from   w  w w.j  ava  2 s . c o  m
    ps.close();
    sock.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Verifier Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField1 = new JTextField();
    JTextField textField2 = new JTextField();
    JTextField textField3 = new JTextField();

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent comp) {
            boolean returnValue;
            JTextField textField = (JTextField) comp;
            try {
                Integer.parseInt(textField.getText());
                returnValue = true;/* w ww  . j  a v a2s. c  o  m*/
            } catch (NumberFormatException e) {
                returnValue = false;
            }
            return returnValue;
        }
    };

    textField1.setInputVerifier(verifier);
    textField3.setInputVerifier(verifier);

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

From source file:MulticastSender.java

public static void main(String[] args) {

    InetAddress ia = null;/*from   w ww  .  j  av a 2  s. c  om*/
    int port = 0;
    String characters = "Here's some multicast data\n";
    byte[] data = new byte[characters.length()];

    // read the address from the command line
    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            //ia = InetAddressFactory.newInetAddress(args[0]);
        }
        port = Integer.parseInt(args[1]);
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSender MulticastAddress port");
        System.exit(1);
    }

    characters.getBytes(0, characters.length(), data, 0);
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    try {
        MulticastSocket ms = new MulticastSocket();
        ms.joinGroup(ia);
        for (int i = 1; i < 10; i++) {
            ms.send(dp, (byte) 1);
        }
        ms.leaveGroup(ia);
        ms.close();
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }

}