Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

Delim. Separated Text File Runtime

Thread view: 
Tarry Waterson - 31 Mar 2005 16:54 GMT
I have written the following method in an attempt to transfer a delimiter
separated text file into a 2D array for processing.

/**

    * Reads in the data from the source file into an array for processing.

    * calls findLengthAndBreadth to ascertain length and breadth of file.

    * The array is 2D and contains the delimited segments of each line of
the file

    *

    * @param  fileName is the name of the file to open

    * @param  delimiter - delimiting each line element

    * @return    String 2D array of delimited strings

    */

   public String[][] readDelimFileToArray(String fileName, String
delimiter)  {

       findLengthAndBreadth(fileName, delimiter); //call this method to set
the length and breadth used below

       wordArray = new String[fileLength][fileBreadth];   //set in the
previous method

       int i = 0;

       //now to read the file into the wordArray

       try{

           String line = openTextFile(fileName).readLine();  //call the
openTextFile

           while (line != null){   //has not reached the end

               wordArray[i] = line.split(delimiter);

               ++i;   //count the lines

               line = inFile.readLine( );   //read the next line

           }  //while

           }  //try

       catch (IOException e) {

           System.out.println(e);

           }

       return wordArray;

       }

It works fine if the delimiter passed as the 'delimiter' parameter in a
comma, whitespace or in fact any letter or string. If I pass a "*" however I
get this runtime error:

java.util.regex.PatternSyntaxException.

It probably has something to do with the split method and perhaps * is a
special character.

Any ideas how I can get over this without too much change in my method?

Many thanks

Tarry Waterson

Ps The openTextFile(fileName) method looks like this:

private BufferedReader openTextFile(String fileName) {

       try{

          File inputFile = new File(fileName);   //create a new vitual
input file

          inData = new FileReader(inputFile); //create a new file reader
with this IP file

          inFile = new BufferedReader(inData);

       } //try

       catch(IOException e) {

           System.out.println(e);

           }  //catch

       return inFile;

       }
sanjay manohar - 01 Apr 2005 01:56 GMT
If you glance at the documentation for String.split, you will see it
uses Regular Expressions. If you look these up (e.g. under
java.util.regex.Pattern - I do advise looking at these: invaluable),
you can see all the special characters you can use - very powerful
stuff!!

But yes, you are right that * is a special character, and you need to
use something like "\*" to do the job.
If you don't know the delimiter at compile time, use Pattern.quote() to
convert a delimiter string to one that can be used safely with split(),
Tarry Waterson - 01 Apr 2005 10:41 GMT
Thanks for that.

Should do the trick

Cheers
Tarry

> If you glance at the documentation for String.split, you will see it
> uses Regular Expressions. If you look these up (e.g. under
[quoted text clipped - 6 lines]
> If you don't know the delimiter at compile time, use Pattern.quote() to
> convert a delimiter string to one that can be used safely with split(),
Tarry Waterson - 01 Apr 2005 16:51 GMT
Sanjay

"\*" does not work. Reports illegal escape character. I also had a look at
using Pattern.quote() but this method is not included in my Pattern package
(I am using SDK 1.4.2_04).

Any other suggestions?

> If you glance at the documentation for String.split, you will see it
> uses Regular Expressions. If you look these up (e.g. under
[quoted text clipped - 6 lines]
> If you don't know the delimiter at compile time, use Pattern.quote() to
> convert a delimiter string to one that can be used safely with split(),
sanjay manohar - 04 Apr 2005 05:01 GMT
Ah! Sorry, I forgot that the parser will also look for escape
sequences. To get a '\' character in a string, you need to use "\\".

So try String delimeter = "\\*";
This string will actually contain the two characters '\' and '*'
Crazy system, I'm glad I didnt invent it
Tarry Waterson - 09 Apr 2005 12:51 GMT
Thanks Sanjay

That is a winner.

Tarry

> Ah! Sorry, I forgot that the parser will also look for escape
> sequences. To get a '\' character in a string, you need to use "\\".
>
> So try String delimeter = "\\*";
> This string will actually contain the two characters '\' and '*'
> Crazy system, I'm glad I didnt invent it


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.