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, int radix) throws NumberFormatException 

Source Link

Document

Parses the string argument as a signed integer in the radix specified by the second argument.

Usage

From source file:io.github.gsteckman.doorcontroller.INA219Util.java

/**
 * Reads the Current from the INA219 with an I2C address and for a duration specified on the command line.
 * /* w  w w  .j a  v  a2  s  .  co m*/
 * @param args
 *            Command line arguments.
 * @throws IOException
 *             If an error occurs reading/writing to the INA219
 * @throws ParseException
 *             If the command line arguments could not be parsed.
 */
public static void main(String[] args) throws IOException, ParseException {
    Options options = new Options();
    options.addOption("addr", true, "I2C Address");
    options.addOption("d", true, "Acquisition duration, in seconds");
    options.addOption("bv", false, "Also read bus voltage");
    options.addOption("sv", false, "Also read shunt voltage");

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);

    Address addr = Address.ADDR_40;
    if (cmd.hasOption("addr")) {
        int opt = Integer.parseInt(cmd.getOptionValue("addr"), 16);
        Address a = Address.getAddress(opt);
        if (a != null) {
            addr = a;
        } else {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("INA219Util", options);
            return;
        }
    }

    int duration = 0;
    if (cmd.hasOption("d")) {
        String opt = cmd.getOptionValue("d");
        duration = Integer.parseInt(opt);
    } else {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("INA219Util", options);
        return;

    }

    boolean readBusVoltage = false;
    if (cmd.hasOption("bv")) {
        readBusVoltage = true;
    }

    boolean readShuntVoltage = false;
    if (cmd.hasOption("sv")) {
        readShuntVoltage = true;
    }

    INA219 i219 = new INA219(addr, 0.1, 3.2, INA219.Brng.V16, INA219.Pga.GAIN_8, INA219.Adc.BITS_12,
            INA219.Adc.SAMPLES_128);

    System.out.printf("Time\tCurrent");
    if (readBusVoltage) {
        System.out.printf("\tBus");
    }
    if (readShuntVoltage) {
        System.out.printf("\tShunt");
    }
    System.out.printf("\n");
    long start = System.currentTimeMillis();
    do {
        try {
            System.out.printf("%d\t%f", System.currentTimeMillis() - start, i219.getCurrent());
            if (readBusVoltage) {
                System.out.printf("\t%f", i219.getBusVoltage());
            }
            if (readShuntVoltage) {
                System.out.printf("\t%f", i219.getShuntVoltage());
            }
            System.out.printf("\n");
            Thread.sleep(100);
        } catch (IOException e) {
            LOG.error("Exception while reading I2C bus", e);
        } catch (InterruptedException e) {
            break;
        }
    } while (System.currentTimeMillis() - start < duration * 1000);
}

From source file:fr.romainf.QRCode.java

public static void main(String[] args) {
    Options options = buildOptions();// www .  j a  v  a  2 s  .co  m

    CommandLineParser parser = new BasicParser();

    try {
        CommandLine cmd = parser.parse(options, args);
        args = cmd.getArgs();

        int l = args.length;
        if (l != 1) {
            System.out.println("Can only encode one datum at a time (" + l + " given)");
            printUsage(options);
            System.exit(1);
        }
        if (cmd.hasOption("help")) {
            printUsage(options);
            System.exit(0);
        }

        String output = cmd.getOptionValue("o", DEFAULT_OUTPUT_FILE);
        String pixelColourText = cmd.getOptionValue("c", DEFAULT_PIXEL_COLOUR);
        if (pixelColourText.startsWith("0x")) {
            pixelColourText = pixelColourText.substring(2);
        }
        Color pixelColour;
        if (pixelColourText.length() == 6) {
            pixelColour = new Color(Integer.parseInt(pixelColourText, 16));
        } else {
            pixelColour = new Color((int) Long.parseLong(pixelColourText, 16), true);
        }

        writeQRCode(args[l - 1], output, pixelColour);

    } catch (ParseException e) {
        System.out.println(e.getMessage());
        printUsage(options);
        System.exit(1);
    } catch (WriterException e) {
        System.out.println("Could not create QRCode from data (" + e.getMessage() + ")");
        System.exit(2);
    } catch (IOException e) {
        System.out.println("Could not save QRCode to file (" + e.getMessage() + ")");
        System.exit(4);
    }
    System.exit(0);
}