Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tokenizer question 1

Status
Not open for further replies.

Kyuss

Programmer
Feb 21, 2001
15
AU
Hi, i am very much a oop newbie.

was just wondering how i solve this simple problem...

i need to split a date for example:

12/05/01

by the '/' into an array.

When i include this in a packaged class, when i call the method i cant return the result as an array: val[0]=12,val[1]=05 etc. I only seem to be able to return a single result. I am calling the class from a jsp page by the way.

Any help much appreciated! and the circus leaves town...
 
Hi!
Do you think something like this?

private int str2int(String s) {
int i=0;

try { i=Integer.parseInt(s);
} catch (Exception e) {}
return i;
}

public int[] splitDate(String date) {
int[] result=new int[3];

result[0]=str2int(date.substring(0, 2));
result[1]=...
result[2]=...
return result;
}

Good luck. Bye, Otto.
 
Hi, Kyuss.

Your problem can be solved by using the methods in String class.
For example, you can create a String object that contains "xx/xx/xx". Then you will be able to get the part of the string you are interested in by using methods like "substring", which gets the specified part you need, "indexOf", which returns the position of the specified character within the string (you could be interested in locating "/") and so on.
If you need to take a look at the API specification of these classes and methods you can find them at:

I hope you find it useful.
Bye from Spain!
 
You want the date returned in 3 pieces. (month, date, year)
I chose to have it in an array.

so call this function like:


String[] split_date;
split_date = split(date, "/");


[tt]
Code:
String [] split(String s, String delim) {
  StringTokenizer st;
  String [] temp;
  int i = 0;

  if(delim == null)
    st = new StringTokenizer(s);
  else
    st = new StringTokenizer(s, delim);

  temp = new String[st.countTokens()];
  
  while(st.hasMoreTokens()) {
    temp[i++] = st.nextToken();
  }
  return temp;
}
[/tt]
I hope this helped! ;-)
- Casey Winans
 
Perfect guys!

Thank you so much!!!

:) and the circus leaves town...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top