Find and Replace Characters in txt File

V

vera13

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

hiwa

(e-mail address removed) ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
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?
 
V

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

Vera

hiwa said:
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 :(
 
H

hiwa

Vera ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
hiwa said:
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.
 
V

Vera

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 */
}
 
V

Vera

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!!!! :)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top