0

I am making a "Code Decoder" - So there are 3 lines, the first is the reference string, the second is jumbled, and then the third is calculated based on matching the first and second strings. Example:

  1. THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

  2. UIFARVJDLACSPXOAGPYAKVNQTAPWFSAUIFAMB ZAEPH

  3. XFABSFAWFSZACBEAQFPQMFAEPJOHAWFSZACBEAUIJOHTAIBAIB

What I'm doing is getting the 3 strings, converting them to char arrays, then looking through the first string, finding where it is in the array of the alphabet, then at that position I enter the letter from the second string on another array at the same place. Therefore, later, I can find a letter in the alphabet and get its corresponding letter in the second array.

Problem - nothing is inputting into the second array... It comes up empty.

Let me know if any clarification is needed.

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter First String:"); // First String
    String first_string = br.readLine();
    System.out.print("Enter Second String:"); // Second String 
    String second_string = br.readLine();
    System.out.print("Enter Third String:"); // Third String 
    String third_string = br.readLine();

    char[] first = first_string.toCharArray();
    char[] second = second_string.toCharArray();
    char[] third = third_string.toCharArray();
    char[] compare_a = {'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'}; //length = 26
    char[] compare_b = new char[26]; //length = 26

    for(int x =0; x < first.length ; x++){// going through first array
        char first_letter = first[x]; // gets letter from first string

        for(int z = 0; z < 26; z++){ // looks through alphabet for the position of the letter
            if(first_letter == compare_a[z]){ // if the letter is found at a certain position
                // System.out.println(first_letter + " = " + compare_a[z] + " " + compare_b[z]  + " " +  second[x]);
                compare_b[z] = second[x]; // insert into the same position on the second array
            }
        }
    }

    for(int t =0; t<26; t++){

        char m = compare_b[t];
        System.out.print(m); //Printing out array of new letters
    }
}

Note:

When I tried outputting the results to a string, it comes out correctly ---

   letters = letters + second[x];

It just doesn't go into the array...

9
  • Use debugger or add println statements to make sure your strings contain what you expect. Commented Jan 11, 2014 at 23:57
  • I did - and all the strings have what I want - but the "compare_b" array isn't including the new values. Commented Jan 11, 2014 at 23:59
  • Step through your code with the debugger. Does the if statement evaluate to false all the time? Commented Jan 12, 2014 at 0:00
  • Actually you don't need to convert string to char array; you can determine position of a character using charAt() method Commented Jan 12, 2014 at 0:00
  • Did the debug line (commented out) inside the if-statement show what you would expect while stepping through the code in the debugger? Commented Jan 12, 2014 at 0:02

1 Answer 1

1

Bug

Should be

compare_b[z] = second[z]; 

but is

compare_b[z] = second[x];

Fixed code

public static void main(String[] args) {

        String first_string = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
        String second_string = "UIFARVJDLACSPXOAGPYAKVNQTAPWFSAUIFAMB ZAEPH";
        String third_string = "XFABSFAWFSZACBEAQFPQMFAEPJOHAWFSZACBEAUIJOHTAIBAIB";

        char[] first = first_string.toCharArray();
        char[] second = second_string.toCharArray();
        char[] third = third_string.toCharArray();
        char[] compare_a = {'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'}; //length = 26
        char[] compare_b = new char[26]; //length = 26

        for (int x = 0; x < first.length; x++) {// going through first array
            char first_letter = first[x]; // gets letter from first string

            for (int z = 0; z < 26; z++) { // looks through alphabet for the position of the letter
                if (first_letter == compare_a[z]) { // if the letter is found at a certain position
                    // System.out.println(first_letter + " = " + compare_a[z] + " " + compare_b[z]  + " " +  second[x]);
                    compare_b[z] = second[z]; // insert into the same position on the second array
                }
            }
        }

        for (int t = 0; t < 26; t++) {

            char m = compare_b[t];
            System.out.print(m); //Printing out array of new letters
        }
    }

Fixed result is

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

4 Comments

Yes - I was fiddling and made it come empty for a second ... However it shouldn't because when I output this: System.out.println(first_letter + " = " + compare_a[z] + " " + compare_b[z] + " " + second[x]); The proper values are there... but they don't input to the compare_b[] array
@Ds.109 compare_b[z] = second[z]; <- change z with x :)
That's great that it inputs into the array! But I want it to input the letter in the same position as in the alphabet --- So, the first U should be in the 19th spot in the array.. so i can match it with the alphabet.
Nevermind -it worked ! I used x and it worked for some reason :)

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.