Hello to all coders out there, any help is appreciated for this is a personal project and not for a school that I thought would be fun, but not turning out to be so. Now the project is to read in an input file, the compare it to a srting[] alphabets = {"a","b","c"...."z"} then to replace the alphabets with another string[] newAlphabets = {"Apple", "Ball", "Cat", .... "Zebra"}. Now I have tried the simple replaceAll() example and it kinda worked. But the plan is to input a .txt file and have the substitution run and output with a new file. JamesBond.txt: Name is Bond, James Bond. Output.txt should be: NoAppleMonkeyElephant InsectSnake BallOpenNoDuck, JungelAppleMonkeyElephantSnake BallOpenNoDuck. Here's what I have so far:
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
public class FnR {
// array of alphabets
static String[] alphabet = {"a","b","c","d","e","f","g","h","i",
"j","k","l","m","n","o","p","q","r","s","t","u",
"v","w","x","y","z"};
// replace the alphabets with substituted string
static String[] newAlphabets =
{"Apple","Bat","Cat","Dog","Egale","Fox","Goat","Horse","Insect","Jungle",
"King","Lion","Monkey","Nose","Open","Push","Quit","Run","Stop","Turn",
"Up","Volume","Water","X-mas","Yes","Zip"};
@SuppressWarnings("resource")
public static void main(String[] arg) throws IOException{
try {
//reads in the file
Scanner input = new Scanner("JamesBond.txt");
File file = new File(input.nextLine());
input = new Scanner(file);
while (input.hasNextLine()) {
String line = input.nextLine();
//prints the txt file out
System.out.println(line);
// replaces the letter j with Jungel in the text regardless of
case
String newtxt1 = line.replaceAll("J","Jungle");
//prints new text
System.out.println(newtxt1);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} }
Output:
Name is Bond, James Bond
Name is Bond, Jungleames Bond