Re: Find and Replace Characters in txt File
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.