Reputation: 19
I need a sequence of a Numbers to be generated through incrementing. For Ex if it starts with 001 goes till 999 next no should be A01 again goes till A99 next should be B01. Can anybody help me to write a logic for this. Should be implemented using Java code.
Upvotes: 0
Views: 4817
Reputation: 3986
Allocate a batch of 100 numbers in one go and use them and allocate a new batch if the existing batch is exhausted. so something like String getBatchPrefix() which generates 0, "A", "B"... then create an object
class BatchIncrementer {
private final String prefix;
private final AtomicInteger incrementer;
BatchIncrementer(String pPrefix) {
incrementer = new AtomicInteger();
prefix = pPrefix;
}
Integer getNext() {
return incementer.getAndIncrement();
}
Boolean hasNext() {
return incrementer.get() < 100;
}
}
Upvotes: 1
Reputation: 10108
are you looking for something like this.. (not tested)
char ch='a';
int iterations=999;
int count=0;
for(int i=0;i<iterations;i++){
if(i%100==0){
ch++;
count=0;
}
System.out.println(ch+""+count);
count++;
}
Upvotes: 0
Reputation: 5619
One way to do this would be to make the first character, and the second 2 characters separately.
public SequenceGenerator {
private int lowerTwoDigits;
private char thirdDigit;
public SequenceGenerator () {
lowerTwoDigits = 0;
thirdDigit = '0';
}
public String nextElement() {
lowerTwoDigits++;
if (lowerTwoDigits == 100) {
lowerTwoDigits = 1;
thirdDigit++;
if (thirdDigit == '9' + 1)
thirdDigit = 'A';
}
String result = thirdDigit +
('0' + lowerTwoDigits / 10 ) +
('0' + lowerTwoDigits % 10 );
return result;
}
}
public class Main {
public static void main(String[] args) {
SequenceGenerator s = new SequenceGenerator();
int MAX = 99 * 36;
while (MAX >= 0) {
System.out.println(s.nextElement());
MAX--;
}
}
}
This should do what you are asking for, and I think you can understand what is going on in the nextElement method.
Upvotes: 1
Reputation: 13960
Here's my (clumsy, yet hopefully correct) attempt:
class Program {
public static void main(String[] args) {
Incrementer inc = new Incrementer();
while (inc.hasNext()) {
System.out.println(inc.getNext());
}
}
}
class Incrementer {
private final static char[] prefixes = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C' };
//all the way to 'Z' if you want, but I'm lazy
private int currentPrefix = 0;
private int currentNumber = 0;
public boolean hasNext() {
if ((currentPrefix == (prefixes.length - 1)) && currentNumber > 99) {
return false;
}
return true;
}
// this may throw ArrayIndexOutOfBoundsException, check hasNext() first
public String getNext() {
String result;
if (currentNumber > 99) {
currentNumber = 1;
currentPrefix++;
}
result = "" + prefixes[currentPrefix];
if (currentNumber <= 9) {
result += "0";
}
result += currentNumber;
currentNumber++;
return result;
}
}
Upvotes: 1
Reputation: 94653
Try this,
class Foo
{
private static int incr=0;
public static String getNo()
{
incr++;
if(incr%100==0)
incr++;
String no=String.valueOf(incr%100);
String str= String.valueOf((char)('A'+incr/100)) + new String(new byte[]{'0','0'},0,2-no.length()) +
String.valueOf(incr%100);
return str;
}
}
public class Test
{
public static void main(String args[]){
for(int i=1;i<=310;i++){
System.out.println(Foo.getNo());
}
}
}
Upvotes: 1
Reputation: 16615
Here is a simple Alphabetic incrementor, im sure you can figure out the rest :-)
public static String incrementAlphabetic(String value, int position) {
value = value.toUpperCase();
char c = value.charAt(position);
c++;
boolean next = false;
if (c > 'Z') {
next = true;
c = 'A';
}
String n = value.substring(0, position)
+ String.valueOf(c)
+ value.substring(position + 1, value.length());
return (next && position > 0) ? incrementAlphabetic(n, position - 1) : n;
}
public static void main(String[] args) {
String start = "A";
for (int i = 0; i < 1000; i++) {
System.out.println(start = incrementAlphabetic(start, 0));
}
}
Upvotes: 0
Reputation: 109823
simple and incompleted,
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();
}
}
Upvotes: 1