Find and Replace Characters in txt File

Discussion in 'Java' started by vera13@gmail.com, Sep 6, 2006.

  1. Guest

    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?
    , Sep 6, 2006
    #1
    1. Advertising

  2. hiwa Guest

    ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > 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?

    Describe your file format, or, data format on the file.
    Is it a text file with ASCII characters?
    hiwa, Sep 6, 2006
    #2
    1. Advertising

  3. Vera Guest

    > 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.
    Vera, Sep 6, 2006
    #3
  4. hiwa Guest

    Vera ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > > 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.

    This may do the job:
    -------------------------------------
    char [][] maps = {{'B', '8'}, {'O', '0'}};
    for (int i = 0; i < maps.length; ++i){
    line = line.replace(maps[0], maps[1]);
    }
    hiwa, Sep 6, 2006
    #4
  5. Vera Guest

    hiwa wrote:
    > This may do the job:
    > -------------------------------------
    > char [][] maps = {{'B', '8'}, {'O', '0'}};
    > for (int i = 0; i < maps.length; ++i){
    > line = line.replace(maps[0], maps[1]);
    > }


    Nope, didn't work :(
    Vera, Sep 6, 2006
    #5
  6. hiwa Guest

    Vera ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > hiwa wrote:
    > > This may do the job:
    > > -------------------------------------
    > > char [][] maps = {{'B', '8'}, {'O', '0'}};
    > > for (int i = 0; i < maps.length; ++i){
    > > line = line.replace(maps[0], maps[1]);
    > > }

    >
    > Nope, didn't work :(

    Should work.
    hiwa, Sep 6, 2006
    #6
  7. Vera Guest

    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[0], maps[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, Sep 7, 2006
    #7
  8. Vera Guest

    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!!!! :)
    Vera, Sep 7, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sameen
    Replies:
    2
    Views:
    432
    Victor Bazarov
    Aug 29, 2005
  2. Íßêïò
    Replies:
    51
    Views:
    1,517
    Íßêïò
    Aug 16, 2010
  3. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,524
    Roedy Green
    Sep 15, 2011
  4. joeyej
    Replies:
    5
    Views:
    93
    Tad McClellan
    Apr 12, 2006
  5. Gilles
    Replies:
    4
    Views:
    257
    Gilles
    Sep 5, 2012
Loading...

Share This Page