Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Setting txt file columns delimiter

    Hi all,

    I have a method to read a txt file with comma separated columns

    Java Code:
    public void importToArray(){
            int rows = 0;
            bin = new String[numberOfLines()][6];
            try {
                 FileReader fr = new FileReader(fileToImport);
                 BufferedReader br = new BufferedReader(fr);
                 String line = null;
    
                 while((line = br.readLine())!= null){
                     StringTokenizer stk = new StringTokenizer(line, ",");
                     while(stk.hasMoreTokens()){
                         for (int cls = 0;cls<6; cls++){
                             bin[rows][cls]= stk.nextToken();
                         }
                         rows++;
                     }//end inner while loop
                 }//end outer while loop
                 br.close();
            }//end try
            catch(Exception e){
                System.out.println(e);
            }
        }
    I main class I have set a GUI where to choose different columns separators, as the attached picture.

    Radio buttons are grouped in a group button so that only one button can be selected.

    How should I modify my method so I can have the selected delimiter from the GUI?

    Thanks.

    Susanna
    Attached Thumbnails Attached Thumbnails Setting txt file columns delimiter-a00060.png  

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default

    I think this is the key

    Java Code:
    StringTokenizer stk = new StringTokenizer(line, ",");
    Maybe you should say something like:

    Java Code:
    StringTokenizer stk = new StringTokenizer(line, userChoice);
    Where user choice is a string. Then before you start reading the file, you can do a check:

    if (commaRadioButton.isSelected()){
    userChoice =","

    }
    else if (SemiColonRadioButton.isSelected()){
    userChoice =";"

    }

    else if (TabRadioButton.isSelected()){
    userChoice ="\t";

    }

    else if (SpaceRadioButton.isSelected()){
    userChoice =" ";

    }

    And in this way you make your tokenizer dynamic. Maybe one of the seniors can take a look, thats my guess.
    Last edited by sehudson; 03-13-2011 at 09:29 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not have the method accept a String parameter which will be your delimiter. Then the method that calls this method can pass in the appropriate delimiter depending on the JRadioButton that has been selected. But again, you should not have GUI specific code in this method, and again, I think that the method should have the file name passed in so that the file name is not hard-coded and the method is more flexible.

  4. #4
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    Hi, thank you both for your replies: I've modified the code with the if else statements as follow

    Java Code:
    public void importToArray(){
            int rows = 0;
            bin = new String[numberOfLines()][6];
            try {
                 FileReader fr = new FileReader(fileToImport);
                 BufferedReader br = new BufferedReader(fr);
                 String line = null;
                 String radioColumnSelect = null;
                 
                 if (jCommaRadioButton.isSelected()){
    radioColumnSelect =",";
    }
    else if (jSemiColonRadioButton.isSelected()){
    radioColumnSelect =";";
    }
    else if (jTabRadioButton.isSelected()){
    radioColumnSelect ="\t";
    }
    else if (jSpaceRadioButton.isSelected()){
    radioColumnSelect =" ";
    }
            while((line = br.readLine())!= null){
                     StringTokenizer stk = new StringTokenizer(line, radioColumnSelect);
                     while(stk.hasMoreTokens()){
                         for (int cls = 0;cls<6; cls++){
                             bin[rows][cls]= stk.nextToken();
                         }
                         rows++;
                     }//end inner while loop
                 }//end outer while loop
                 br.close();
            }//end try
            catch(Exception e){
                System.out.println(e);
            }
        }
    First thing I declare a String field

    String radioColumnSelect = null;

    then I pass this as argument in

    StringTokenizer stk = new StringTokenizer(line, radioColumnSelect);

    The problem is that all RadioButtons are in main class and are all declared as private, so I get error because it can't see in this class all RadioButtons variables.

    Susanna
    Attached Thumbnails Attached Thumbnails Setting txt file columns delimiter-a00067.jpg  

  5. #5
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    May be instead of checking isSelected in this class, in your main class itself apply a listener for the radio buttons and call the importToArray() method in a little different way.

    //comma radio button clicked
    importToArray(',');

    and others

    your imporToArray method may go somewhat like this
    public void importToArray(char delimit){
    //the delimit variable contains the delimiting character
    }
    Swastik

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    The problem is that all RadioButtons are in main class and are all declared as private, so I get error because it can't see in this class all RadioButtons variables.

    I think you can reference the buttons if you put them into an array which is public like this:

    Java Code:
    public JRadioButton[] myRadioButtons = new JRadioButton[4];
    myRadioButtons[0] = commaRadioButton;
    myRadioButtons[1] = SemiColonRadioButton;
    myRadioButtons[2] = TabRadioButton;
    myRadioButtons[3] = SpaceRadioButton;

    And then in your other class reference them from the array:

    Java Code:
    for (JRadioButton jrb:myMainClass.myRadioButtons) {
      if (jrb.isSelected()) {
        //your code
      }
      ...
    }
    Last edited by ozzyman; 03-14-2011 at 10:42 AM.

Similar Threads

  1. setting path and classpath variables from batch file -
    By manojsingh.manoj@gmail.co in forum Advanced Java
    Replies: 1
    Last Post: 09-15-2009, 11:50 AM
  2. scanning a file and using more than one delimiter
    By thomase in forum New To Java
    Replies: 20
    Last Post: 04-07-2009, 03:04 AM
  3. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 10:50 PM
  4. Log4j - setting up name of log file
    By Virkant Pawar in forum New To Java
    Replies: 3
    Last Post: 06-03-2008, 07:39 AM
  5. Servlet sending the file (setting header)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-27-2008, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •