Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

splitting filename into variables with string tokenizer (CEHJ?)

Posted on 2005-05-13
14
Medium Priority
?
564 Views
Last Modified: 2010-07-27
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
0
Comment
Question by:daveleblanc
  • 8
  • 3
  • 3
14 Comments
 

Author Comment

by:daveleblanc
ID: 13996218
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13996252
A StringTokenizer is actually *less* flexible. What was wrong with the way i suggested splitting the filename?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13996257
>>A StringTokenizer is actually *less* flexible.

(Than String.split that is)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Expert Comment

by:zzynx
ID: 13996287
       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
 
LVL 37

Expert Comment

by:zzynx
ID: 13996301
>> parts = new String[tok.countTokens()];

should have been:

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

0
 
LVL 37

Expert Comment

by:zzynx
ID: 13996304
If you don't mind I answer instead of CEHJ ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13996336
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
 

Author Comment

by:daveleblanc
ID: 13996375
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13996418
>> i wanted to hear other opinions on it
See my previous comment
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13996425
>> so how this looking?
Where's "parts" initialized?
0
 

Author Comment

by:daveleblanc
ID: 13996457
guess i havent yet, example?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13996460
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
 
LVL 37

Accepted Solution

by:
zzynx earned 150 total points
ID: 13996491
>> guess i havent yet, example?

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

0
 
LVL 37

Expert Comment

by:zzynx
ID: 14013794
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question