Question

I don't understand why I am able to print these string values with a very similar loop, but it doesn't go through and replace the letters as I'd assume the replace method would do. Here's the output: aforementioned output --but as you may plainly see, the phrase which is meant to be translated isn't. I print the arrays and the values which line up with each other. The input is the reprinted (unchanged) though it is the string value which is meant to be adjusted to morse. I would highly appreciate if anyone might be able to explain what the issue is.

The text value in the .txt file is as follows, as well:

.-
-...
-.-.
-..
.
..-.
--.
....
..
.---
-.-
.-..
--
-.
---
.--.
--.-
.-.
...
-
..-
...-
.--
-..-
-.--
--..
.----
..---
...--
....-
.....
-....
--...
---..
----.
-----

and then the class:

import java.util.*;
import java.io.*;
public class MorseCode
{
    public static void main(String []args) throws IOException
    {
        String characters[] = {"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
        String morseCharacter[] = new String[(characters.length)];

        Scanner in = new Scanner(System.in);
        System.out.println();

        System.out.print("Enter a phrase you wish to translate to morse: ");
        String entry = in.nextLine();

        File fileName1 = new File("morseCode.txt");

        Scanner inFile = new Scanner(fileName1);

        int counter1 = 0;
        while(inFile.hasNextLine() && counter1 < (characters.length))
        {
            morseCharacter[counter1] = inFile.nextLine();
            morseCharacter[counter1] += " ";
            counter1++;
        }

        String result = "";

        for(int x = 0; x < (morseCharacter.length); x++)
        {
            result = entry.replaceAll(characters[x], morseCharacter[x]);
        }

        for(int x = 0; x < (morseCharacter.length); x++)
        {
            System.out.println(characters[x] + " = " + morseCharacter[x]);
        }

        System.out.println(result);
    }
}
Was it helpful?

Solution

You're only applying the very last character because you always replace from entry, not from your previous result.

Try this:

    String result = entry;
    //              ^^^^^

    for(int x = 0; x < (morseCharacter.length); x++)
    {
        result = result.replaceAll(characters[x], morseCharacter[x]);
    } //         ^^^^^^

OTHER TIPS

You should wrap your code with reading and replacing into loop. Something like this:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class MorseCode {
    public static void main(String[] args) throws IOException {
        String characters[] = {"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};

        int counter1 = 0;
        Scanner inFile = new Scanner(new File("morseCode.txt"));
        String morseCharacter[] = new String[(characters.length)];
        while (inFile.hasNextLine() && counter1 < (characters.length)) {
            morseCharacter[counter1] = inFile.nextLine();
            morseCharacter[counter1] += " ";
            counter1++;
        }

        for (int x = 0; x < (morseCharacter.length); x++) {
            System.out.println(characters[x] + " = " + morseCharacter[x]);
        }

        Scanner in = new Scanner(System.in);
        System.out.print("\nEnter a phrase you wish to translate to morse: ");

        while (in.hasNextLine()) {
            String line = in.nextLine();

            for (int x = 0; x < (morseCharacter.length); x++) {
                line = line.replaceAll(characters[x], morseCharacter[x]);
            }

            System.out.println(line);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top