Click to See Complete Forum and Search --> : setting tokens to vars - is it possible


annie613
11-21-2005, 09:45 AM
is there away to set tokens to vars individually,

here is my code

String loadid = files[i].toString();
loadid = loadid.substring(0, loadid.length()-4); //drop extension
String input = loadid;
StringTokenizer st = new StringTokenizer(input, "\\", true);

while(st.hasMoreTokens())
{
String token = st.nextToken();
if(token.startsWith("\"") && token.endsWith("\""))
{
token = token.substring(1,token.length()-1);
}//if
System.out.println(token.toString());
}//while

here is the output

C:
\
MyFolder
\
test
\
ids
\
unload
\
14481-1-0-8FAA-1234-5678

what i really want is the last token
14481-1-0-8FAA-1234-5678 to be set to
String test = "14481-1-0-8FAA-1234-5678";
is there away to set tokens to vars?? thanks in advance

Khalid Ali
11-21-2005, 02:18 PM
well if you want the last value in the tokens, then its prety easy.. just see the bold+green lines that I am adding in ur code...


String loadid = files[i].toString();
loadid = loadid.substring(0, loadid.length()-4); //drop extension
String lastToken=""; String input = loadid;
StringTokenizer st = new StringTokenizer(input, "\\", true);

while(st.hasMoreTokens())
{
String token = st.nextToken();
if(token.startsWith("\"") && token.endsWith("\""))
{
token = token.substring(1,token.length()-1);
}//if

lastToken = token.toString()
System.out.println(lastToken );

}//while

//and here you can use the lastToken variable for whatever ..:-)

System.out.println(lastToken );



its a simple and crude way of doing things, but if it can do what u want then its good. However if your tokens list can contain ton of entries then it won't be as efficient

annie613
11-22-2005, 06:14 AM
thanks - thats all i needed. its a small application and this will be effiiecent enough to run the 2 times i will need it too. i was trying to overcomplicate the process by setting it to a String[] or a Map. Thanks! :) Cheers

Khalid Ali
11-22-2005, 09:13 AM
you are welcome...just make a habbit of coming back to your code often and you will notice that how many times your code could have been simpler but was written with a degree of complexity...:-)

annie613
11-22-2005, 09:32 AM
i have another question about the tokens.

i am trying to take the lastToken which is equal to 12345-1-0-ABCD-1234-5678

and use a Tokenizer on this to break this up by hypens
i use this code


String segment = lastToken.toString();
StringTokenizer st1 = new StringTokenizer(segment, "-", true);

while(st1.hasMoreTokens())
{

String t = st1.nextToken();

if(t.startsWith("-") && t.endsWith("-"))
{

t = t.substring(1, t.length()-1);//java.lang.StringIndexOutOfBoundsException: String index out of range: -1
System.out.println("t " + t); //only prints hypens

}
}

im getting an outOfBoundsError or i just get hypens printed.
can you make a suggestion on how to resolve this or at least explain what I am trying to do. i am confusing myself? :(

annie613
11-22-2005, 11:22 AM
simple mistake --- tokenizer returns the hyphens as 1-character Strings


String segment = lastToken.toString();
StringTokenizer st1 = new StringTokenizer(segment, "-", true);

while(st1.hasMoreTokens())
{
String t = st1.nextToken();
System.out.println(t);
}


now my output is

12345
-
1
-
0
-
ABCD
-
1234
-
5678

which is what i wanted. but is there a way to set these to vars? i think that is what my orginal question was in the beginning of the post.

because i want to take the first segment 12345 and set it to a var
String var1 = //something
so i can use it in a SQL query

i was trying to put t into an ArrayList but im still having trouble???

Khalid Ali
11-22-2005, 12:41 PM
you can put them in a list e.g

String segment = lastToken.toString();
StringTokenizer st1 = new StringTokenizer(segment, "-", true);
java.util.List list = new java.util.ArrayList();
while(st1.hasMoreTokens())
{
String t = st1.nextToken();
list.add(t);
System.out.println(t);
}

//here you have a list object that has all the values.
//You can access them either by the index number (if u know already)
//for e.g to get first value u will need to do this
String firstValue = String.valueOf(list.get(0));

//you can put error handling above to make sure that u do have data in th elist before referencing the index etc...

annie613
11-23-2005, 05:59 AM
thanks it worked! CHEERS


String segment = lastToken.toString();
StringTokenizer st1 = new StringTokenizer(segment, "-", true);

ArrayList seg = new ArrayList();

while(st1.hasMoreTokens())
{
String t = st1.nextToken();

if(!t.startsWith("-") && !t.endsWith("-"))
{
seg.add(t);
}//if
}//while

String agid = (String)seg.get(0);
String prinode = (String)seg.get(1);
String secnode = (String)seg.get(2);
String objname = (String)seg.get(3);
String firstdate = (String)seg.get(4);
String lastdate = (String)seg.get(5);