#1
  1. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0

    Seperating a variable into arr w/o split


    My company's server is running on java 1.2.2, so split() isn't available.

    What I need to do is take a date (7-10-2003) and seperate it into 3 seperate values and store those into an array.

    What i was thinking of doing was using indexOf(-) to get the locatin of each, then substring to grab each piece.

    Does anyone know of a more efficient way of doing this?
    Last edited by StevenC; July 11th, 2003 at 02:49 PM.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    10
    Rep Power
    0
    For functionality like the JavaScript split() function, check out java.util.StringTokenizer. It breaks a string into tokens based on your desired delimiter and provides nice methods for iterating over them. It may not be more efficient than the method you've described, but it's more convenient.
  4. #3
  5. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    What method? I don't know what tokens are. (Sorta new to jsp)
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    10
    Rep Power
    0
    By "tokens" I just mean the substrings between the delimiters. So, if your string was "7-10-2003" and your delimiter was "-", your tokens would be:

    "7"
    "10"
    "2003"

    You construct a StringTokenizer by passing it the string and the delimiter you want to use. Then you use its methods to get its tokens, one by one. It has a method called hasMoreTokens() which tells you if there are any tokens left, and a method called nextToken() which gives you the next one in the list. Here's an example of how to use StringTokenizer to break the string down into these tokens:

    // sample code
    String date = "7-10-2003";
    StringTokenizer tokens = new StringTokenizer( date, "-" );
    String month = "", day = "", year = "";
    if ( tokens.hasMoreTokens() )
    month = tokens.nextToken();
    if ( tokens.hasMoreTokens() )
    day = tokens.nextToken();
    if ( tokens.hasMoreTokens() )
    year = tokens.nextToken();

    System.out.println( month );
    System.out.println( day );
    System.out.println( year );
    // end sample

    The code above would print:

    7
    10
    2003

    on the output line.

    You can read more about StringTokenizer here.
  8. #5
  9. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    No one else has any suggestions?
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    You could either use String.indexOf(int ch) and String.indexOf(int ch, int fromIndex) or use the StringTokenizer with 1.2. Personally I would probably use the StringTokenizer as the code would be a little cleaner, but really it is personal preference.

    EDIT: I suppose you could make a Date object out of it if you want to. Either use the Date constructor or use DateFormat.parse(String). Not sure if they would take the date the way it is formatted in that string or not. If not, you would have to use SimpleDateFormat. If you get a Date object you can then use it's getter methods (getMonth, etc.) to get your values, or you could use the newer Calendar object with the Date to get these values (They deprecated the Date objects methods in favor of rewriting how dates are accessed with the new Calendar object. Personally I still don't like the Calendar object, but whatever).

    I would probably suggest against this approach just because java's date manipulation classes are some of java's worst features, but it would let you validate the date as being a legal value.
    Last edited by Nemi; July 13th, 2003 at 03:57 PM.
  12. #7
  13. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Ok, I used my own code for the date, and that worked fine, but it won't work for seperating a delimited list of id's. So, i'm trying to use the tokens idea. I get this error
    Code:
    Unable to compile class for JSP/opt/netscape/server4/https-web/config/../ClassCache/_jsps/_infosys/_infra/_software/_database/_calendar/_do_del_jsp.java:261: Attempt to reference method hasMoreTokens in class java.util.StringTokenizer as an instance variable.
                                    while(tokens.hasMoreTokens)
                                                ^
    With this code
    Code:
    String delete_ids = request.getParameter("del_vals");
    String[] del_arr = null;
    StringTokenizer tokens = new StringTokenizer(delete_ids, ",");
    
    out.println(delete_ids + "<br/>");
    int i = 0;
    while(tokens.hasMoreTokens)
    {
    	del_arr[i] = tokens.nextToken();
    	i++;
    }
  14. #8
  15. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Change
    Code:
    while(tokens.hasMoreTokens)
    to
    Code:
    while(tokens.hasMoreTokens())
    ~ishnid
    Last edited by ishnid; July 14th, 2003 at 09:46 AM.
  16. #9
  17. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Ya, blind me saw that.
    Now i get the wonderful NullPointerException! :|

IMN logo majestic logo threadwatch logo seochat tools logo