Find and Replace Characters in txt File

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

  1. vera13

    vera13 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

    So, how would I tell it to check for letters in the string and replace
    certain letters for certain numbers?
    vera13, Sep 6, 2006
    1. Advertisements

  2. vera13

    hiwa Guest

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

  3. vera13

    Vera Guest

    Describe your file format, or, data format on the file.
    Sorry, it's a txt file with 1 number per line, like this:


    Vera, Sep 6, 2006
  4. vera13

    hiwa Guest

    Vera ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
    This may do the job:
    hiwa, Sep 6, 2006
  5. vera13

    Vera Guest

    Nope, didn't work :(
    Vera, Sep 6, 2006
  6. vera13

    hiwa Guest

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

    Should work.
    hiwa, Sep 6, 2006
  7. vera13

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

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

    // Store number in array

    // Sort the vector elements

    // Print sorted vector (comma-separated)

    // Print sorted vector elements
    for(int i=0; i<tokens.size(); i++)

    // Close the file

    catch(Exception exception)

    } /* End Main Method */
    Vera, Sep 7, 2006
  8. vera13

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.