user7820948
user7820948

Reputation:

Change every letter of a REALLY long string

I'm trying to make a program that changes every letter to something else i.e a=q, b=w, c=e and so on...

The program works fine when i insert smaller Strings, but when i try to read a file and input the returned String to my method, it does not translate the letters correctly. My method looks like this:

    public static String converString(String conversionString) {

    String letters = "abcdefghijklmnopqrstuvwxyz";
    String encodedLetters = "qwertyuiopasdfghjklzxcvbnm";

   conversionString = conversionString.toLowerCase();

    char[] letterChar = letters.toCharArray();
    char[] encChar = encodedLetters.toCharArray();

    char[] stringChar = conversionString.toCharArray();

    for (int i = 0; i < stringChar.length; i++) {

        for (int j = 0; j < letterChar.length; j++) {
            if(letterChar[j] == stringChar[i]) {
                conversionString = conversionString.replace(letterChar[j], encChar[j]);
            }
        }

    }
    return conversionString;

}

It works fine if i input a small string for it to convert, but when i read a BIG text file(done like this:)

public class ReadFile {


public String readFile(Charset encoding) throws IOException {

    {
        byte[] encoded = Files.readAllBytes(Paths.get("HugeTextFile.txt"));
        return new String(encoded, encoding);
    }

it doesnt work properly(it works fine if the text file is small, but the text i want to convert is 1k+ lines)

Here's a sample output of some of the text i get, when i try to convert a big file:

   qlqqq'l qqqqfqqqql qf qqfqqqlqfq

lqqql qqqqqll

qqqqqqq q. qqqf qqq qqqqqq-qqlq

qlqqq qql qqqqffqfq qq qqq qqqf qqqqq qf lqqqqfq qf qqq lqlqqq qf qqq
qqfq, qfq qf qqqqfq fqqqqfq qq qq: qfqq qq qqqqq lqq qqq qqqqqq qfqq qqq
qqqq qqq lqlqqq qql qqqqqfq, qqq qq qqq fq qqqqqqql qq qqfqqqlqqqqfl qf
qq, 'qfq qqqq ql qqq qlq qf q qqqq,' qqqqqqq qlqqq 'qqqqqqq qqqqqqql qq
qqfqqqlqqqqfl?'

lq lqq qql qqflqqqqqfq qf qqq qqf qqfq (ql qqll ql lqq qqqlq, fqq qqq

But the text itself does not use that many a's, as this is outputting. Can anyone help me explain why this is happening with big texts only?

Upvotes: 1

Views: 346

Answers (2)

Yevgen
Yevgen

Reputation: 1667

The problem is that you are replacing already replaced characters. As far as I understand, you want to change letters in your string according to the "dictionary" you provided. Using data structures you are using, this problem can be solved like this:

public static String convertString(String conversionString) {

  String letters =        "abcdefghijklmnopqrstuvwxyz";
  String encodedLetters = "qwertyuiopasdfghjklzxcvbnm";

  conversionString = conversionString.toLowerCase();

  char[] letterChar = letters.toCharArray();
  char[] encChar = encodedLetters.toCharArray();

  char[] stringChar = conversionString.toCharArray();

  StringBuilder sb = new StringBuilder();

  for (int i = 0; i < stringChar.length; i++) {
    int j = letters.indexOf(stringChar[i]); 
    sb.append(conversionString.substring(i, i + 1).replace(letterChar[j], encChar[j]));   
  }

  return sb.toString();
}

Upvotes: 1

sandy
sandy

Reputation: 529

Try this.

for(int i=0;i<stringChar.length;i++){
    stringChar[i]=encChar[stringChar[i]-97];
}

Upvotes: 1

Related Questions