Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How do I make a 'find and replace' program in Java?
I have developed a special code using simple symbols that I can use to form letters which combine to form words. When I want to translate something into my code, it takes me a long time because I have to do it all by hand (not quite as slow as it used to be because now I have memorized all the symbol combinations). Because of this slow translation speed, I want to make a Java program to help me out. This is what I want it to do:
The window of the program will be laid out in BreezySwing GUI. If possible, I would like to stay away from complicated syntax in my program; that is why I use BreezySwing. The window will have two text areas, and one button. The user will type whatever they want into the upper text area and make it just like they want it. When ready, the user will click the button in between the two text areas. This will activate a method in the program that will translate the user's text into my code.
One possible method for translation is find and replace. I got this idea from the find and replace feature in Microsoft Office. It works by searching for a string in a document, and then inserting something else in its place. In my case, my program could somehow search the user's input for all the different letters, numbers, and symbols and then determine what the coded letter, number, or symbol would be. This is the part of my program that I need help with.
Here is an example:
Assume that the user's input is "Find and Replace".
Once the translation method is activated, the program will start searching the input for matching characters. It could begin with the letters, and then end with other symbols like period, comma, question mark, space, etc. When it finds matches, it will start displaying the translation in the second text area of the program's window. When done with a specific letter, number, or symbol, it will move on to the next character.
Guaranteed ten points for best solution.
2 Answers
- Mark aka jack573Lv 71 decade agoFavorite Answer
Hi, the first thing you would want to do is make two arrays containing the letters you want to find, and the letters (or symbols) you want to replace.
You should make the arrays the same length, and have the symbols in the same position as the letters.
For instance,
String letters[] = {"a", "b", "c", ... };
String symbols[] = {"!", "@", "#", ... };
In the above example, if you find 'a', you replace it with '!', 'b' with '@', etc.
Then you make a for loop that goes through the first array (letters).
for (int i = 0; i < letters.length; i++)
In the for loop, you want to use a method of the string class called replaceAll
Just asy the string you have got as input is called input, for no better name.
input = input.replaceAll( letters[i], symbols[i] );
Now, the only problem will be is if the characters in the symbols can be found in the letters. It may replace the symbols by more symbols, and produce a wrong result.
I did not say how to use BreezySwing to get the input, as you should be used to that.
Good luck.
- Anonymous5 years ago
Open the file, read it into a Java string, use the String replace methods or Java's regular expressions. Overwrite the original file. That's the belligerent way. The better way would be to open the file, read each line in one at a time, look for the text you want to replace, if found, write out the change (remember to back the file pointer back to the start of the line.