Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Find and Replace Characters in txt File

Reply
Thread Tools

Find and Replace Characters in txt File

 
 
vera13@gmail.com
Guest
Posts: n/a
 
      09-06-2006
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?

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      09-06-2006
(E-Mail 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?

 
Reply With Quote
 
 
 
 
Vera
Guest
Posts: n/a
 
      09-06-2006
> 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.

 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      09-06-2006
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[i][0], maps[i][1]);
}

 
Reply With Quote
 
Vera
Guest
Posts: n/a
 
      09-06-2006
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[i][0], maps[i][1]);
> }


Nope, didn't work

 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      09-06-2006

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[i][0], maps[i][1]);
> > }

>
> Nope, didn't work

Should work.

 
Reply With Quote
 
Vera
Guest
Posts: n/a
 
      09-07-2006
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.

 
Reply With Quote
 
Vera
Guest
Posts: n/a
 
      09-07-2006
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!!!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
[2.5.1] Read each line from txt file, replace, and save? Gilles Python 4 09-05-2012 12:52 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
Replace and inserting strings within .txt files with the use of regex Python 51 08-16-2010 07:45 AM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM



Advertisments