splitting filename into variables with string tokenizer (CEHJ?)

i need to read a dynamic filename in a directory, split the filename into 3 seperate parts and assign two of the 3 parts to variables

example filename: CRS Apr05 exclude.csv

variables v_agency_Sname should = CRS
and v_process_Action should = exclude

can someone provide an example of this using string tokenizer
Who is Participating?
zzynxConnect With a Mentor Software engineerCommented:
>> guess i havent yet, example?

>>  parts = fileName.split("[ .]");
String parts[] = fileName.split("[ .]");

daveleblancAuthor Commented:
CEHJ im trying the current method you suggested but i need something a bit more managable as i wont always be the person supporting this application and need it a but more "user friendly" use of tokenizer was suggested

A StringTokenizer is actually *less* flexible. What was wrong with the way i suggested splitting the filename?
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

>>A StringTokenizer is actually *less* flexible.

(Than String.split that is)
zzynxSoftware engineerCommented:
       String fileName = "CRS April05 Writeoff.csv";
        StringTokenizer tok = new StringTokenizer(fileName, " .");
        parts = new String[tok.countTokens()];
        int index = 0;
        while (tok.hasMoreTokens())
            parts[index++] = tok.nextToken();
        System.out.println(parts[0] + "-" + parts[1] + "-" + parts[2] + "-" + parts[3]);
zzynxSoftware engineerCommented:
>> parts = new String[tok.countTokens()];

should have been:

String parts[] = new String[tok.countTokens()];

zzynxSoftware engineerCommented:
If you don't mind I answer instead of CEHJ ;°)
zzynxSoftware engineerCommented:
Quote from the docs:
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

daveleblancAuthor Commented:
really well i guess i was misinformed

i really need to get back on the java bandwagon

nothing was wrong with your example CEHJ im using it right now just some suggested tokenizer and i wanted to hear other opinions on it

so how this looking?

public class GetValues {

      private static final int EXIT_FAILURE = 1;
      private static final String DELIMETER = " ";

            // Default configuration information.
      private static final String v_agency_Sname; //CRS
      private static final String v_agency_Fname; // Credit Recovery Services
      private static final String v_agency_phone; //18889997777
      private static final String v_process_action; //exclude or return
      private static final String v_note_text; //note to be posted to banner
      private static final String v_inputfile;
      private static final String v_outputfile;
      // Opens the collections csv file, reads it in, splits filename into variables
      //and reads col/row A1 for config file notetext
      public static void main(String args[]) throws Exception
      //read file placed in directory
      FilenameFilter filter = new FilenameFilter() {
                  public boolean accept(File dir, String name) {
                        return name.toLowerCase().endsWith(".csv");
      String[] fileNames = new File(data).list(filter); // (where directoryName is your starting dir)
      String csvFile = fileNames[0]; // range check it though

      //split filename and assign variables
      String fileName = csvFile;
               parts = fileName.split("[ .]");
               System.out.println(parts[0] + "-" + parts[1] + "-" + parts[2] + "-" + parts[3]);
      //set other variables based on filename
      if (parts[0].equals("CRS")) {
                        v_agency_Fname = "Credit Recovery Services";
                        v_agency_phonenum = "18006732203";
                   } else if (parts[0].equals("CRM")) {
                        v_agency_Fname = "Corporate Risk Managment";
                        v_agency_phonenum = "18002670490";
                   } else if (parts[0].equals("FDR")) {
                        v_agency_Fname = "Financial Debt Recovery";
                        v_agency_phonenum = "18007633328";
                //set process action either exclude or return
                  if (parts[2].toLowerCase().equals("exclude")) {
                     doWriteOff = true;
                     doReturnAccount = false;
                   } else if (parts[2].toLowerCase().equals("return")){
                     doReturnAccount = true;
                     doWriteOff = false;

zzynxSoftware engineerCommented:
>> i wanted to hear other opinions on it
See my previous comment
zzynxSoftware engineerCommented:
>> so how this looking?
Where's "parts" initialized?
daveleblancAuthor Commented:
guess i havent yet, example?
Seems OK. Your if..elses won't scale well though. I'd be inclined to map keys, of 'CRS', 'CRM' etc. to values of type Agency:

class Agency {
    private String fname; // (choose a better name)
    private String phoneNumber;
zzynxSoftware engineerCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.