Is there any method in java to know size of decimal??
I mean 14 has 2 digits , 100 has 3 etc.......

I m going to implement auto generated number like
A0001 , now i want to change it on each new entry , so Does any one has any great idea???

Auto generated number length remain fix...

Recommended Answers

All 3 Replies

are meaning

A&14 == A00014
A&150 == A00150

don't do it that, that really stupid idea,

subString() or String.format

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class GenerateProgresId {

    private static String[] aChar = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    private static List<String> listA;

    public GenerateProgresId() {
        List<Number> list3 = new ArrayList<Number>();
        List<Integer> list4 = new ArrayList<Integer>();
        List<? extends Number> list5 = new ArrayList<Integer>();
        List<? extends Number> list6 = list4;
        list3.add(Integer.valueOf(333));
        list4.add(Integer.valueOf(444));
        //list5.add(Integer.valueOf(555)); // compile error
        //list6.add(Integer.valueOf(666)); // compile error
        Number n3 = list3.get(0);
        Integer i4 = list4.get(0);
        Number n5 = !list5.isEmpty() ? list5.get(0) : null;
        Number n6 = list6.get(0);
        System.out.printf("%s, %s, %s, %s%n", n3, i4, n5, n6);
        int pomocInt = 1;
        String pomocStr = "";
        if (pomocInt < 10) {
            pomocStr = "0" + pomocInt;
        } else if (pomocInt < 100) {
            pomocStr = String.valueOf(pomocInt);
        } else {
            pomocStr = String.valueOf(pomocInt);
        }
        System.out.println(pomocStr);
        pomocInt = 12;
        pomocStr = "";
        if (pomocInt < 10) {
            pomocStr = "0" + pomocInt;
        } else if (pomocInt < 100) {
            pomocStr = String.valueOf(pomocInt);
        } else {
            pomocStr = String.valueOf(pomocInt);
        }
        System.out.println(pomocStr);
        pomocInt = 157;
        pomocStr = "";
        if (pomocInt < 10) {
            pomocStr = "0" + pomocInt;
        } else if (pomocInt < 100) {
            pomocStr = String.valueOf(pomocInt);
        } else if (pomocInt > 99) {
            pomocStr = String.valueOf(pomocInt);
            pomocStr = pomocStr.substring(pomocStr.length() - 2, pomocStr.length());
        }
        System.out.println(pomocStr);
        listA = new ArrayList<String>();
        listA.addAll(Arrays.asList(aChar));
        System.out.println("From List");
        System.out.println(GenerateProgresIdList(null));
        System.out.println(GenerateProgresIdList(""));
        System.out.println(GenerateProgresIdList("B"));
        System.out.println(GenerateProgresIdList("AA"));
        System.out.println(GenerateProgresIdList("AZ"));
        System.out.println(GenerateProgresIdList("ZY"));
        System.out.println(GenerateProgresIdList("ZZ"));
        System.out.println("From String[]");
        System.out.println(GenerateProgresIdString(null));
        System.out.println(GenerateProgresIdString(""));
        System.out.println(GenerateProgresIdString("B"));
        System.out.println(GenerateProgresIdString("AA"));
        System.out.println(GenerateProgresIdString("AZ"));
        System.out.println(GenerateProgresIdString("ZY"));
        System.out.println(GenerateProgresIdString("ZZ"));
    }

    public static String GenerateProgresIdList(String str) {
        int lastChar = aChar.length - 1;
        String retStr = "AA";
        if (str != null) {
            if (str.length() > 0) {
                if (str.length() == 1) {
                    retStr = str + aChar[0];
                } else if (str.length() == 2) {
                    int stChar = listA.indexOf(str.substring(0, 1));
                    int ndChar = listA.indexOf(str.substring(1, str.length()));
                    if ((stChar != lastChar) || (ndChar != lastChar)) {
                        if (ndChar == lastChar) {
                            retStr = listA.get(stChar + 1) + listA.get(0);
                        } else {
                            retStr = listA.get(stChar) + listA.get(ndChar + 1);
                        }
                    }
                }
            }
        }
        return retStr;
    }

    public static String GenerateProgresIdString(String str) {
        String lastChar = aChar[aChar.length - 1];
        String retStr = "AA";
        if (str != null) {
            if (str.length() > 0) {
                if (str.length() == 1) {
                    retStr = str + aChar[0];
                } else if (str.length() == 2) {
                    if ((!str.substring(0, 1).equals(lastChar)) || (!str.substring(1, str.length()).equals(lastChar))) {
                        String pos1 = str.substring(0, 1);
                        String pos2 = str.substring(1, str.length());
                        if ((pos2).equals(lastChar)) {
                            int heplInt = 0;
                            for (int i = 0; i < aChar.length; i++) {
                                if (aChar[i].equals(pos1)) {
                                    heplInt = i + 1;
                                    break;
                                }
                            }
                            retStr = aChar[heplInt] + aChar[0];
                        } else {
                            int heplInt = 0;
                            for (int i = 0; i < aChar.length; i++) {
                                if (aChar[i].equals(pos2)) {
                                    heplInt = i + 1;
                                    break;
                                }
                            }
                            retStr = pos1 + aChar[heplInt];
                        }
                    }
                }
            }
        }
        return retStr;
    }

    public static void main(String[] args) {
        GenerateProgresId gpi = new GenerateProgresId();
    }
}

number length remain fix

Look at the String format method.

Well @mKorbel i didnt thought that way too.... :) I have developed a easy algorithem to do it without extra work. Clean and clear code....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.