Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 12th, 2006, 2:42 PM   #1
Gumby
Newbie
 
Join Date: Mar 2006
Location: Andover, MA / Rochester, NY / Nanticoke, PA
Posts: 16
Rep Power: 0 Gumby is on a distinguished road
Send a message via AIM to Gumby
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.
Gumby is offline   Reply With Quote
Old Jun 12th, 2006, 3:01 PM   #2
pushkarajthorat
Java Developer
 
pushkarajthorat's Avatar
 
Join Date: Jun 2006
Location: Solapur, India.
Posts: 24
Rep Power: 0 pushkarajthorat is an unknown quantity at this point
Send a message via Yahoo to pushkarajthorat
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();
}
pushkarajthorat is offline   Reply With Quote
Old Jun 12th, 2006, 5:15 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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.
Jimbo is offline   Reply With Quote
Old Jun 12th, 2006, 8:15 PM   #4
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
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.
nindoja is offline   Reply With Quote
Old Jun 12th, 2006, 8:28 PM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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...
Jimbo is offline   Reply With Quote
Old Jun 13th, 2006, 9:27 AM   #6
Gumby
Newbie
 
Join Date: Mar 2006
Location: Andover, MA / Rochester, NY / Nanticoke, PA
Posts: 16
Rep Power: 0 Gumby is on a distinguished road
Send a message via AIM to Gumby
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.
Gumby is offline   Reply With Quote
Old Jun 13th, 2006, 2:23 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to 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;
}
titaniumdecoy is offline   Reply With Quote
Old Jun 13th, 2006, 2:58 PM   #8
Gumby
Newbie
 
Join Date: Mar 2006
Location: Andover, MA / Rochester, NY / Nanticoke, PA
Posts: 16
Rep Power: 0 Gumby is on a distinguished road
Send a message via AIM to Gumby
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!
Gumby is offline   Reply With Quote
Old Jun 13th, 2006, 3:03 PM   #9
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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));
}
Jimbo is offline   Reply With Quote
Old Jun 13th, 2006, 3:14 PM   #10
Gumby
Newbie
 
Join Date: Mar 2006
Location: Andover, MA / Rochester, NY / Nanticoke, PA
Posts: 16
Rep Power: 0 Gumby is on a distinguished road
Send a message via AIM to Gumby
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:
Gumby is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:27 PM.

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