Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Determine if the current s is a scientific notation or not
     * The format of scientific notation is as follows:
     * 1e19, 3.1e3, 1e-2, .3e4, 3E4E5E
     * @param s
     * @return
     */
    public static boolean isScientificNotation(String s) {
        if (s == null || s.length() == 0)
            return false;
        String[] arr = s.toLowerCase().split("e");
        if (arr.length == 0 || !(isDecimal(arr[0]) || isInteger(arr[0])))
            return false;
        for (int i = 1; i < arr.length; i++) {
            if (!isInteger(arr[i]))
                return false;
        }
        return true;
    }

    /**
     * Note: the decimal here doesn't contain integer, i.e., we consider 1.0,
     * 0.1, .3 as decimals, rather than 1, 3 or 4
     * 
     * @param s
     * @return
     */
    public static boolean isDecimal(String s) {
        if (s == null || s.length() == 0)
            return false;
        int dot = s.indexOf('.');
        //If there's no occurence of ., return false;
        if (dot == -1)
            return false;
        //If there are many occurrences of ., return false;
        if (s.indexOf('.', dot + 1) != -1)
            return false;

        return isInteger(s.substring(0, dot - 1)) && isNonNegativeInteger(s.substring(dot + 1));

    }

    /**
     * Determine if the current s is an integer or not
     * 
     * @param s
     * @return
     */
    public static boolean isInteger(String s) {
        if (s == null || s.length() == 0)
            return false;
        for (int i = 0; i < s.length(); i++) {
            if (i == 0 && s.charAt(i) == '-')
                continue;
            if (s.charAt(i) > '9' || s.charAt(i) < '0')
                return false;
        }
        return true;
    }

    /**
     * Determine if the current s is a positive integer or not
     * @param s
     * @return
     */
    public static boolean isNonNegativeInteger(String s) {
        if (s == null || s.length() == 0)
            return false;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) > '9' || s.charAt(i) < '0')
                return false;
        }
        return true;
    }
}