0

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
1
  • Very sorry about that, my mistake. Commented Apr 10, 2017 at 5:09

1 Answer 1

1

Not going to give you the code. This answer is providing hints for you to write the code yourself.

Don't use replaceAll(). You'd have to do it 26 times, and that is not really a good solution.

Instead, create a StringBuilder for building up the result string. Then loop through the characters of the input string with a normal for loop using String methods length() and charAt().

Now here is the main "trick" that will simplify your code. Java stores text in Unicode, where letters a to z are stored consecutively. That means you can calculate the index into your newAlphabets array. To do that, you'll write code like this:

char ch = ...;
if (ch >= 'a' && ch <= 'z') {
    int idx = ch - 'a';  // number between 0 and 25, inclusive
    // use idx here
} else if (ch >= 'A' && ch <= 'Z') {
    int idx = ch - 'A';  // number between 0 and 25, inclusive
    // use idx here
} else {
    // not a letter
}

Hope this helps you write the code yourself.

Sign up to request clarification or add additional context in comments.

2 Comments

Oops! Typo fixed.
Thank you so much for your help Andreas I will try to work it out like you suggested and get back to you guys. So any help or suggestion in the future is appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.