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
daveleblancAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
>> guess i havent yet, example?

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

0
 
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

opinions?
0
 
CEHJCommented:
A StringTokenizer is actually *less* flexible. What was wrong with the way i suggested splitting the filename?
0
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.

 
CEHJCommented:
>>A StringTokenizer is actually *less* flexible.

(Than String.split that is)
0
 
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]);
0
 
zzynxSoftware engineerCommented:
>> parts = new String[tok.countTokens()];

should have been:

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

0
 
zzynxSoftware engineerCommented:
If you don't mind I answer instead of CEHJ ;°)
0
 
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.

0
 
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;
                  }
   
      

0
 
zzynxSoftware engineerCommented:
>> i wanted to hear other opinions on it
See my previous comment
0
 
zzynxSoftware engineerCommented:
>> so how this looking?
Where's "parts" initialized?
0
 
daveleblancAuthor Commented:
guess i havent yet, example?
0
 
CEHJCommented:
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;
   
    ...
}
0
 
zzynxSoftware engineerCommented:
thanks
0
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.