Example usage for java.lang NumberFormatException NumberFormatException

List of usage examples for java.lang NumberFormatException NumberFormatException

Introduction

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

Prototype

public NumberFormatException() 

Source Link

Document

Constructs a NumberFormatException with no detail message.

Usage

From source file:by.bsu.zmiecer.PieChartDemo1.java

/**
 * Creates a sample dataset.//www. j  a  va2 s . co  m
 * 
 * Source: http://www.bbc.co.uk/news/business-15489523
 *
 * @return A sample dataset.
 */
private PieDataset createDataset() {
    DefaultPieDataset dataset = new DefaultPieDataset();
    try {

        File file = new File(
                "C:\\Users\\\\\\GitHub\\BSU\\2 course\\Practical Training\\Week 5\\Task53\\input.txt");
        Scanner sc = new Scanner(file);
        while (sc.hasNext()) {
            String name;
            if (sc.hasNext())
                name = sc.next();
            else
                throw new InputMismatchException();
            Double val;
            if (sc.hasNext()) {
                val = Double.valueOf(sc.next());
                if (val < 0)
                    throw new NumberFormatException();
            } else
                throw new InputMismatchException();
            dataset.setValue(name, val);
        }
    } catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(getParent(), "File not found!");
    } catch (InputMismatchException e) {
        JOptionPane.showMessageDialog(getParent(), "Enter corret data!");
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(getParent(), "Enter corret data!");
    }
    return dataset;
}

From source file:org.vetmeduni.tools.cmd.CommonOptions.java

/**
 * Get the default number of threads if the command line does not contain the parallel option; if it is contain,
 * parse the command line and return the number of threads asked for
 *
 * @param cmd the command line where check if the option is set
 *
 * @return the number of threads to use/* www. j a  v  a  2 s  . c  o m*/
 * @throws org.vetmeduni.tools.ToolNames.ToolException if the option is not numeric
 */
public static int numberOfThreads(Log logger, CommandLine cmd) {
    try {
        int nThreads = DEFAULT_THREADS;
        if (cmd.hasOption(parallel.getOpt())) {
            nThreads = Integer.parseInt(getUniqueValue(cmd, parallel.getOpt()));
            if (nThreads != 1) {
                // TODO: change when real multi-thread is implemented
                logger.warn(
                        "Currently multi-threads does not control the number of threads in use, depends on the number of outputs");
            } else if (nThreads < 0) {
                throw new NumberFormatException();
            }
        }
        return nThreads;
    } catch (NumberFormatException e) {
        throw new ToolNames.ToolException("--" + parallel.getLongOpt() + " should be a positive integer");
    }
}

From source file:ISO8601DateTimeFormat.java

/**
 * Parse the time zone./*from   w  w  w  .j a va 2s .c  om*/
 * @param i The position to start parsing.
 * @param text The text to parse.
 * @return The position after parsing has finished.
 */
protected final int parseTZ(int i, String text) {
    if (i < text.length()) {
        // check and handle the zone/dst offset
        int offset = 0;
        if (text.charAt(i) == 'Z') {
            offset = 0;
            i++;
        } else {
            int sign = 1;
            if (text.charAt(i) == '-') {
                sign = -1;
            } else if (text.charAt(i) != '+') {
                throw new NumberFormatException();
            }
            i++;

            int offsetHour = Integer.valueOf(text.substring(i, i + 2)).intValue();
            i += 2;

            if (text.charAt(i) != ':') {
                throw new NumberFormatException();
            }
            i++;

            int offsetMin = Integer.valueOf(text.substring(i, i + 2)).intValue();
            i += 2;
            offset = ((offsetHour * 60) + offsetMin) * 60000 * sign;
        }
        int offsetCal = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);

        calendar.add(Calendar.MILLISECOND, offsetCal - offset);
    }
    return i;
}

From source file:de.akquinet.gomobile.androlog.test.PostReporterTest.java

public void testPostWithLongLog() {
    Log.init(getContext());/*w ww . j av a2 s  .c  o m*/
    String message = "This is a INFO test";
    String tag = "my.log.info";
    Log.d(tag, message);
    Log.i(tag, message);
    Log.w(tag, message);
    for (int i = 0; i < 200; i++) {
        Log.w("" + i);
    }
    List<String> list = Log.getReportedEntries();
    Assert.assertNotNull(list);
    Assert.assertFalse(list.isEmpty());
    Assert.assertEquals(25, list.size());

    Log.report();
    Log.report("this is a user message", null);
    Exception error = new MalformedChallengeException("error message", new NumberFormatException());
    Log.report(null, error);
}

From source file:Ascii.java

/**
 * Parses an unsigned long from the specified subarray of bytes.
 * @param b the bytes to parse//  w  w  w.ja v a2  s.co m
 * @param off the start offset of the bytes
 * @param len the length of the bytes
 * @exception NumberFormatException if the long format was invalid
 */
public static long parseLong(byte[] b, int off, int len) throws NumberFormatException {
    int c;

    if (b == null || len <= 0 || !isDigit(c = b[off++])) {
        throw new NumberFormatException();
    }

    long n = c - '0';
    long m;

    while (--len > 0) {
        if (!isDigit(c = b[off++])) {
            throw new NumberFormatException();
        }
        m = n * 10 + c - '0';

        if (m < n) {
            // Overflow
            throw new NumberFormatException();
        } else {
            n = m;
        }
    }

    return n;
}

From source file:de.haber.xmind2latex.cli.CliParameters.java

/**
 * Creates a {@link XMindToLatexExporter} for the given arguments.
 * /*from w  w  w . ja  va 2 s.c o m*/
 * @param args Arguments to configure this {@link XMindToLatexExporter}.
 * 
 * @return A created {@link XMindToLatexExporter} or null, if no {@link CliParameters#INPUT} parameter is used.
 * 
 * @throws ParseException, NumberFormatException for invalid arguments
 * @throws ConfigurationException for invalid input files
 * @throws IllegalArgumentException if the given input file does not exist
 */
public static XMindToLatexExporter build(String[] args) throws ParseException {
    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args, false);

    if (cmd.getOptions().length == 0) {
        throw new ParseException("Parameter -" + INPUT + " expected.");
    }

    if (cmd.hasOption(VERSION)) {
        printVersion();
    }

    CliParameters.validateNumberOfArguments(cmd, INPUT, options);
    if (cmd.hasOption(INPUT)) {
        File in = new File(cmd.getOptionValue(INPUT));
        Builder builder = new Builder(in);
        if (cmd.hasOption(FORCE)) {
            CliParameters.validateNumberOfArguments(cmd, FORCE, options);
            builder.overwritesExistingFiles(true);
        }

        File out;
        if (cmd.hasOption(OUTPUT)) {
            CliParameters.validateNumberOfArguments(cmd, OUTPUT, options);
            out = new File(cmd.getOptionValue(OUTPUT));
            builder.withTargetFile(out);
        }

        if (cmd.hasOption(TEMPLATE_LEVEL)) {
            CliParameters.validateNumberOfArguments(cmd, TEMPLATE_LEVEL, options);

            String level = cmd.getOptionValue(TEMPLATE_LEVEL);
            try {
                int levelAsInt = Integer.parseInt(level);
                if (levelAsInt < 0) {
                    throw new NumberFormatException();
                }
                builder.withMaxLevel(levelAsInt);
            } catch (NumberFormatException e) {
                ParseException ex = new ParseException(
                        "The level argument of option " + TEMPLATE_LEVEL + " has to be a positive integer.");
                ex.addSuppressed(e);
                throw ex;
            }

        }
        if (cmd.hasOption(HELP)) {
            CliParameters.validateNumberOfArguments(cmd, HELP, options);

            showHelp();
        }

        if (cmd.hasOption(ENVIRONMENT)) {
            CliParameters.validateNumberOfArguments(cmd, ENVIRONMENT, options);

            String[] env = cmd.getOptionValues(ENVIRONMENT);
            for (int i = 0; i + 2 < env.length; i = i + 3) {
                String level = env[i];
                String start = env[i + 1];
                String end = env[i + 2];
                try {
                    int levelAsInt = Integer.parseInt(level);
                    builder.withEnvironmentTemplates(levelAsInt, start, end);
                } catch (NumberFormatException e) {
                    ParseException ex = new ParseException(
                            "The level argument of option " + ENVIRONMENT + " has to be an integer.");
                    ex.addSuppressed(e);
                    throw ex;
                }
            }
        }
        if (cmd.hasOption(LEVEL)) {
            CliParameters.validateNumberOfArguments(cmd, LEVEL, options);

            String[] tmp = cmd.getOptionValues(LEVEL);

            for (int i = 0; i + 1 < tmp.length; i = i + 2) {
                String level = tmp[i];
                String template = tmp[i + 1];
                try {
                    int levelAsInt = Integer.parseInt(level);
                    builder.withTemplate(levelAsInt, template);
                } catch (NumberFormatException e) {
                    ParseException ex = new ParseException(
                            "The level argument of option " + LEVEL + " has to be an integer.");
                    ex.addSuppressed(e);
                    throw ex;
                }
            }
        }
        return builder.build();
    } else {
        return null;
    }
}

From source file:gedi.util.MathUtils.java

/**
 * Throws an exception if n is either a real or to big to be represented by a byte.
 * @param n//from   w ww . j a  v a2 s.c o  m
 * @return
 */
public static int intValueExact(Number n) {
    if (n instanceof Integer || n instanceof Short || n instanceof Byte)
        return n.intValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        if (l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE)
            return (int) l;
    }
    throw new NumberFormatException();
}

From source file:configurator.Configurator.java

/**
 * Method for transform ip text to list of ip addresses. Check recording
 * method and select the method of conversion.
 *
 * @param ipText List of ip ranges, subnets and single ip
 * @return list of ip addresses/*w  w  w.ja va  2 s . c  om*/
 */
static List<String> parseIp(String ipText) {
    Scanner ipReader = new Scanner(ipText);
    List<String> ipList = new ArrayList<>();
    String ipLine;
    while (ipReader.hasNext()) {
        ipLine = ipReader.nextLine().trim();
        if (ipLine.contains("/")) {
            ipList.addAll(parseIpSubnet(ipLine));
        } else if (ipLine.contains("-")) {
            ipList.addAll(parseIpRange(ipLine));
        } else if (validateIP(ipLine)) {
            ipList.add(ipLine);
        } else {
            throw new NumberFormatException();
        }
    }
    return ipList;
}

From source file:Ascii.java

public static long parseLong(char[] b, int off, int len) throws NumberFormatException {
    int c;//  w  w w  .ja va  2 s  . c o m

    if (b == null || len <= 0 || !isDigit(c = b[off++])) {
        throw new NumberFormatException();
    }

    long n = c - '0';
    long m;

    while (--len > 0) {
        if (!isDigit(c = b[off++])) {
            throw new NumberFormatException();
        }
        m = n * 10 + c - '0';

        if (m < n) {
            // Overflow
            throw new NumberFormatException();
        } else {
            n = m;
        }
    }

    return n;
}

From source file:gedi.util.MathUtils.java

/**
 * Throws an exception if n is either a real or to big to be represented by a byte.
 * @param n/*  w  w  w .jav  a2 s  .  c  o m*/
 * @return
 */
public static long longValueExact(Number n) {
    if (n instanceof Long || n instanceof Integer || n instanceof Short || n instanceof Byte)
        return n.longValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        return l;
    }
    throw new NumberFormatException();
}