Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   generating character sequence (http://www.programmingforums.org/showthread.php?t=10310)

Gumby Jun 12th, 2006 3:42 PM

generating character sequence
 
Does anybody know if there's a Java class out there to automatically generate character sequences (i.e., a, b, c, d, e, ..., aa, ab, ac, ...)? I'm going to be generating some SQL statements which could possibly get quite long, depending on the user input, which would be in the form of:
:

SELECT a.ID, a.this, a.that, b.this, b.that, c.this, c.that, etc...
FROM
  (SELECT DISTINCT ID, this, that
  FROM table_name
  WHERE conditions) a,
  (SELECT DISTINCT ID, this, that
  FROM table_name
  WHERE conditions) b,
  (SELECT DISTINCT ID, this, that
  FROM table_name
  WHERE conditions) c
WHERE a.ID = b.ID
  AND a.ID = c.ID;

I need to be able to automatically generate names like this, and it's possibly there could be more than 26 (hence, the need for aa, ab, etc). If there aren't any classes to do this, does anyone have a suggestion on how to write a method to do this?

Thank you in advance.

pushkarajthorat Jun 12th, 2006 4:01 PM

i have got a function "parsetext"
this function gets a string; devides it into tokens; replace tokens with the specified data; and create the required pattern...

this function is part of project "PageDown" on SourceForge.net in a source file "frmWebDataS.java"

you may refer this function to generate the sequence which you require..


private void ParseText()
{
ParsedData.removeAllElements();
int StaticText=0;
int TagText=0;
Vector<String> tmpTokentext=new Vector<String>();

StringTokenizer begintokens = new StringTokenizer(txtDownExp.getText(),"<");
while (begintokens.hasMoreTokens())
{
tmpTokentext.add(begintokens.nextToken());
}
if (tmpTokentext.size()==0) return;
boolean FirstIsToken=tmpTokentext.get(0).startsWith("<");

for (int i=0;i<tmpTokentext.size();i++ )
{
if (!FirstIsToken && i==0)
{
ParsedData.add(new ParsedText(tmpTokentext.get(i),null));
StaticText++;
continue;
}
StringTokenizer endtokens = new StringTokenizer(tmpTokentext.get(i),">");
if (endtokens.countTokens()>2)
{
MainApp.LogWrite(frmName , "Parsing error, too many closing (>) tag...");
}
ParsedData.add(new ParsedText(endtokens.nextToken(),new frmTagItem(MainApp)));
TagText++;
//TokenText.add(endtokens.nextToken());
if (endtokens.countTokens()>0)
{
ParsedData.add(new ParsedText(endtokens.nextToken(),null));;
StaticText++;
}
}
MainApp.LogWrite(frmName , "Parsing done (Text: "+StaticText+", Tag: "+TagText+")");
UpdateTagView();
}

Jimbo Jun 12th, 2006 6:15 PM

I don't know of anything to specifically generate sequences like that, but you could probably come up with your own fairly easy. Afterall, it's just like using base-26 numbering, without any arabic numerals, i.e.
:

String makeStr(int num)
{
  String s = ('a' + num / 26);
  s += ('a' + num % 26);
  return s;
}

I didn't test this or anything, but as long as you have less than 26^2 strings, it'll get you the basic idea.

nindoja Jun 12th, 2006 9:15 PM

Jimbo, I don't think that works, unless i'm misunderstaning what it is supposed to do. When I put 25 in for num, I get "az", and the 25th one should be 'y', which is 27 away from "az".

OP: You may consider doing it like you would do a time converter. Given seconds convert to the correct number of hours, minutes, and seconds. These situations appear to me to be very similar.

Jimbo Jun 12th, 2006 9:28 PM

My code would do everything with 2 letters (you could add a condition for if the original value was less than 26). That said, the z was correct if you start counting from 0, as most programmers tend to do. Perhaps I should have commented that...

Gumby Jun 13th, 2006 10:27 AM

Thanks guys, here's what I went with:
:

private static String makeStr(int num) {
  int r,s=0;
  r = ('a' + num / 26);
  s = ('a' + num % 26);
  String s1 = new Character((char)r).toString();
  String s2 = new Character((char)s).toString();
 
  return s1+s2;
}

I appreciate the help - this is way simpler than what I had written.

titaniumdecoy Jun 13th, 2006 3:23 PM

You can simplify this function as follows, such that you don't declare unnecessary variables.

:

private static String makeStr(int num) {
    String s1 = String.valueOf((char)('a' + num / 26));
    String s2 = String.valueOf((char)('a' + num % 26));
    return s1 + s2;
}


Gumby Jun 13th, 2006 3:58 PM

Quote:

Originally Posted by titaniumdecoy
You can simplify this function as follows, such that you don't declare unnecessary variables.

:

private static String makeStr(int num) {
    String s1 = String.valueOf((char)('a' + num / 26));
    String s2 = String.valueOf((char)('a' + num % 26));
    return s1 + s2;
}


And this is why I love this site. Thank you!

Jimbo Jun 13th, 2006 4:03 PM

Quote:

Originally Posted by titaniumdecoy
You can simplify this function as follows, such that you don't declare unnecessary variables.

:

private static String makeStr(int num) {
    String s1 = String.valueOf((char)('a' + num / 26));
    String s2 = String.valueOf((char)('a' + num % 26));
    return s1 + s2;
}


You can simplify this function as follows, such that you don't declare unnecessary variables. Although the compiler will probably come up with what you did (or very similar). :p
:

private static String makeStr(int num) {
    return String.valueOf((char)('a' + num / 26)) + String.valueOf((char)('a' + num % 26));
}


Gumby Jun 13th, 2006 4:14 PM

Quote:

Originally Posted by Jimbo
You can simplify this function as follows, such that you don't declare unnecessary variables. Although the compiler will probably come up with what you did (or very similar). :p
:

private static String makeStr(int num) {
    return String.valueOf((char)('a' + num / 26)) + String.valueOf((char)('a' + num % 26));
}


$1 to the person who can simplify it even more :banana:


All times are GMT -5. The time now is 8:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC