jueves, 8 de mayo de 2014

A simple way to determine if a String could be an integer

There are a lot of ways to check if a String could represent an integer value. I will show you two very simple and yet fast methods:

Method 1

    public final static boolean isInteger(String in)
    {
        char c;
        int length = in.length();
        boolean ret = length > 0;
        int i = ret && in.charAt(0) == '-' ? 1 : 0;
        for (; ret && i < length; i++)
        {
            c = in.charAt(i);
            ret = (c >= '0' && c <= '9');
        }
        return ret;
    }


Method 2 (modified version of method 1)

    public static boolean isInteger(String in)
    {
        if (in != null)
        {
            char c;
            int i = 0;
            int l = in.length();
            if (l > 0 && in.charAt(0) == '-')
            {
                i = 1;
            }
            if (l > i)
            {
                for (; i < l; i++)
                {
                    c = in.charAt(i);
                    if (c < '0' || c > '9')
                        return false;
                }
                return true;
            }
        }
        return false;
    }


Method 3 

    static final byte[] b = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    public final static boolean isInteger(String in)
    {
        char c;
        int length = in.length();
        int i = 0;
        if (length > 0 && in.charAt(0) == '-')
            i = 1;
        if (i >= length)
        {
            return false;
        }
        for (; i < length; i++)
        {
            c = in.charAt(i);
            if (b[c & 0xff] == 0)
                return false;
        }
        return true;
    }



I hope this information is useful.

PS. Be aware that all of these will say that very large strings (with valid big integers) are true, so if you just try to convert them to Integers, an Exception could be thrown.

domingo, 4 de mayo de 2014

Multiple column list using HTML (ul and li)

I know that you could find a lot of ways to show columns using HTML,  but sometimes for most of those ways you need a "deep"  knowledge of CSS, if you just know the very basic stuff of CSS and HTML, then this is for you. Fast and easy.

The idea is having a multicolumn list, using ul and li tags (and just a little bit of CSS).
Let's suppose you have the following data and you want them to show as a column for every category:

id  name         category
--------------------------
1   Antena       Exterior
2   Emblema      Exterior
3   Llantas      Exterior
4   Luces        Exterior
5   Estéreo      Interior
6   Tapetes      Interior
7   Encendedor   Interior
8   Gato         Accesorio
9   Extintor     Accesorio
10  Herramientas Accesorio
11  Claxon       Cofre
12  Batería      Cofre
13  Filtro aire  Cofre


You want them to look like this:

Exterior   Interior    Accesorio     Cofre
---------  ----------  ------------  -----------
Antena     Estéreo     Gato          Claxon
Emblema    Tapetes     Extintor      Batería
Llantas    Encendedor  Herramientas  Filtro aire
Luces

So, the HTML code could be as easy as this:

<ul style="float:left;list-style-type: none;">Exterior<hr>
    <li>Antena</li>
    <li>Emblema</li>
    <li>Llantas</li>
    <li>Luces</li>
</ul>

<ul style="float:left;list-style-type: none;">Interior<hr>
    <li>Estéreo</li>
    <li>Tapetes</li>
    <li>Encendedor</li>
</ul>

<ul style="float:left;list-style-type: none;">Accesorio<hr>
    <li>Gato</li>
    <li>Extintor</li>
    <li>Herramientas</li>
</ul>

<ul style="float:left;list-style-type: none;">Cofre<hr>
    <li>Claxon</li>
    <li>Batería</li>
    <li>Filtro aire</li>
</ul>




There you have It!.

I hope this is useful for you.