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 / General / September 2006

Tip: Looking for answers? Try searching our database.

Find and Replace Characters in txt File

Thread view: 
vera13@gmail.com - 06 Sep 2006 04:25 GMT
I have to read in a file which has mostly normal floating point numbers
and 2 numbers to throw you off: 41B.495 and 87O.078. The program is
supposd to catch those and replace the B with 8 and the O with 0. I
have the numbers read into a vector array and as they are read in they
are converted to double, but of course it runs into problems with
letters.

So, how would I tell it to check for letters in the string and replace
certain letters for certain numbers?
hiwa - 06 Sep 2006 04:29 GMT
ver...@gmail.com のメッセージ:

> I have to read in a file which has mostly normal floating point numbers
> and 2 numbers to throw you off: 41B.495 and 87O.078. The program is
[quoted text clipped - 5 lines]
> So, how would I tell it to check for letters in the string and replace
> certain letters for certain numbers?
Describe your file format, or, data format on the file.
Is it a text file with ASCII characters?
Vera - 06 Sep 2006 05:58 GMT
> Describe your file format, or, data format on the file.
> Is it a text file with ASCII characters?

Sorry, it's a txt file with 1 number per line, like this:

3434.298
24.2092
3083.40

etc.
hiwa - 06 Sep 2006 06:44 GMT
Vera のメッセージ:

> > Describe your file format, or, data format on the file.
> > Is it a text file with ASCII characters?
[quoted text clipped - 6 lines]
>
> etc.
This may do the job:
-------------------------------------
char [][] maps = {{'B', '8'}, {'O', '0'}};
for (int i = 0; i < maps.length; ++i){
 line = line.replace(maps[i][0], maps[i][1]);
}
Vera - 06 Sep 2006 19:37 GMT
> This may do the job:
> -------------------------------------
> char [][] maps = {{'B', '8'}, {'O', '0'}};
> for (int i = 0; i < maps.length; ++i){
>   line = line.replace(maps[i][0], maps[i][1]);
> }

Nope, didn't work :(
hiwa - 06 Sep 2006 23:31 GMT
Vera のメッセージ:

> > This may do the job:
> > -------------------------------------
[quoted text clipped - 4 lines]
>
> Nope, didn't work :(
Should work.
Vera - 07 Sep 2006 02:00 GMT
Maybe I'm doing something wrong... Here's what I have:
-------------------------------------------------------------------------
public class test
{
   /* Main Method */
   public static void main(String args[]) throws IOException
   {

        // Promt the user for file name
        BufferedReader console = new BufferedReader(new
InputStreamReader(System.in));
        System.out.println("Enter the file name: ");
        String fileName = console.readLine();

        // Create a file object
        File file = new File(fileName);

        // Declare variables
        StringTokenizer tokenizer;
        String line, element="";
        Vector tokens = new Vector();
        int e1=0;

        try
        {
            // Start reading the file
            FileReader fr = new FileReader(file);
            BufferedReader inFile = new BufferedReader(fr);

            // Read the file till EOF
            while((line = inFile.readLine())!= null)
             {
                tokenizer = new StringTokenizer(line);

                 // Checks how many separate parts of the string
                 e1 = tokenizer.countTokens();

                 for(int count=0; count < e1; count++)
                 {
                     element = (String)tokenizer.nextToken();
                     Double elementD = Double.parseDouble(element);

                     // Check for characters
                    char [][] maps = {{'B', '8'}, {'O', '0'}};
                    for (int i = 0; i < maps.length; ++i)
                    {
                        line = line.replace(maps[i][0], maps[i][1]);
                    }

                     // Print number
                     System.out.println(element);

                    // Store number in array
                    tokens.add(element);
                 }
             }

             // Sort the vector elements
             Collections.sort(tokens);

             // Print sorted vector (comma-separated)
             System.out.println(tokens);

            // Print sorted vector elements
            for(int i=0; i<tokens.size(); i++)
            {
                System.out.println((String)tokens.elementAt(i));
            }

            // Close the file
            inFile.close();
        }

        catch(Exception exception)
        {
            System.out.println(exception);
        }

    } /* End Main Method */
}
-------------------------------------------------------------------------

And it's giving me a NumberFormatException once it gets to the number
with B instead of 8. That's when it stops.
Vera - 07 Sep 2006 02:34 GMT
Nevermind, my mistake. I forgot to replace "element" with "line" which
is what I should've used in the first place. Anyway, it works, now.

Thank you SOOOOOOO much!!!! :-)


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.