用for循环将英语翻译成莫尔斯码

时间:2015-02-17 22:22:47

标签: java arrays

我已经彻底搜索过,但在这个网站上找不到满意的答案。 我的任务是将一串英文大写字母翻译成莫尔斯电码。

我的计划是遍历字符串并将数组中的(i)索引与带有莫尔斯代码索引的字母匹配。

import java.util.Scanner;

public class Morse { 
    public static void main(String[]arg) {
        Scanner Read = new Scanner(System.in);
        String uinput, res = "";

        String[] Eng = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z"};
        String[] mors = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};

        System.out.println("Ange den text du vill översätta till Morse-kod: ");
        uinput = Read.nextLine();  //uinput = user input 
        for (int i = 0; i < uinput.length(); ) {
            res = res + (mors.charAt(Eng.indexOf(i)));
        }
        System.out.println(res);
    }
}

当我运行时,我收到错误消息:

Morse.java:16: error: cannot find symbol
  res = res + (mors.charAt(Eng.indexOf(i)));
                                  ^
  symbol:   method indexOf(int)
  location: variable Eng of type String[]
1 error

6 个答案:

答案 0 :(得分:3)

你有两个问题。

首先,你不是在看uinput本身的角色。您需要遍历这些字符以构建输出。

第二个是内置的Java数组没有indexOf()方法。你有几个选择:

  1. 你可以自己写一个。
  2. 您可以将Eng数组的内容放入List(可能是ArrayList),它具有indexOf()方法。
  3. 由于Java char也是一个数字,因此您可以使用该字母本身作为Morse Code数组的索引。 (但请注意案例问题,并注意'A'的数值不是0。)

答案 1 :(得分:1)

看起来您正在尝试在阵列上使用String方法。我认为正确的方法是循环遍历数组并检查每个数组的字符。也就是说,替换:

  for (int i=0; i<uinput.length();)
  {
  res = res + (mors.charAt(Eng.indexOf(i)));
  }

使用之类的内容

for (int i=0; i<uinput.length(); i++)
{
    for(int j=0; j<Eng.length(); j++){
        if(uipunt.charAt(i) == Eng[j]) {
           res = res + (mors[j]);
           continue;
        }
    }
}

这还没有经过测试,但希望能让你走上正确的道路

答案 2 :(得分:1)

您看到的错误是调用不存在的方法的结果。使用方括号[]引用数组。要获得索引i,我们会说:

Eng[i]

就个人而言,我会推迟使用ASCII代码来识别mores-code的索引。在这种情况下,字母“A”在ASCII中为65。 “A”是数组中的索引零。所以索引char的ASCII值(大写)减去65。

import java.util.Scanner;

public class MorseCode {

    private static final String[] MORS = { ".-", "-...", "-.-.", "-..", ".",
            "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
            "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--",
            "-..-", "-.--", "--.." };

    public static void main(String... args) {

        try (Scanner read = new Scanner(System.in)) {

            System.out.print("Morse-Code It (type \"exit\" to exit): ");

            String text = "";
            while (!"exit".equals(text.toLowerCase())) {
                text = removeNonLetters(read.nextLine());
                System.out.println(toMors(text));
            }
        }
    }

    private static String removeNonLetters(String str) {
        return str.replaceAll("[^A-Za-z]", "");
    }

    private static int getIndex(char c) {
        return Character.toUpperCase(c)-65;
    }

    private static String toMors(String str) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < str.length(); i++) {
            sb.append(MORS[getIndex(str.charAt(i))]);
            sb.append("   ");
        }

        return sb.toString();
    }
}

答案 3 :(得分:0)

charAt不是调用字符串数组的有效方法。使用ArrayList而不是数组,你应该能够使用索引。

答案 4 :(得分:0)

我想感谢大家的答案,他们帮助我创建了最终代码:

import java.util.Scanner;
public class Morse
{ 
 public static void main(String[]arg)
 {
  Scanner Read = new Scanner(System.in);
  String uinput,utskr = "";


  String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z"};
  String[] mors = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
  System.out.println("Ange den text du vill översätta till Morse-kod: ");
  uinput = Read.nextLine();
  for (int i=0; i<uinput.length();)
  {
  char currentChar = uinput.charAt(i);
  int charIndex = letters.indexOf(currentChar);
  utskr = utskr + mors[charIndex] + " ";
  }

  System.out.println(utskr);

 }
}

答案 5 :(得分:0)

Java代码

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestProgram {

    public static HashMap<String, String> alphaMorse = new HashMap<String, String>();

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub
        alphaMorse.put("A", ".-");
        alphaMorse.put("B", "-...");
        alphaMorse.put("C", "-.-.");
        alphaMorse.put("D", "-..");
        alphaMorse.put("E", ".");
        alphaMorse.put("F", "..-.");
        alphaMorse.put("G", "--.");
        alphaMorse.put("H", "....");
        alphaMorse.put("I", "..");
        alphaMorse.put("J", ".---");
        alphaMorse.put("K", "-.-");
        alphaMorse.put("L", ".-..");
        alphaMorse.put("M", "--");
        alphaMorse.put("N", "-.");
        alphaMorse.put("O", "---");
        alphaMorse.put("P", ".--.");
        alphaMorse.put("Q", "--.-");
        alphaMorse.put("R", ".-.");
        alphaMorse.put("S", "...");
        alphaMorse.put("T", "-");
        alphaMorse.put("U", "..-");
        alphaMorse.put("V", "...-");
        alphaMorse.put("W", ".--");
        alphaMorse.put("X", "-..-");
        alphaMorse.put("y", "-.--");
        alphaMorse.put("z", "--..");
        alphaMorse.put("1", ".----");
        alphaMorse.put("2", "..---");
        alphaMorse.put("3", "...--");
        alphaMorse.put("4", "....-");
        alphaMorse.put("5", ".....");
        alphaMorse.put("6", "-....");
        alphaMorse.put("7", "--...");
        alphaMorse.put("8", "---..");
        alphaMorse.put("9", "----.");
        alphaMorse.put("0", "-----");
        alphaMorse.put(" ", "|");

        String st = "hello";

    String morse="";
        String convertedMorseCode=toMors(st);
        System.out.print(convertedMorseCode);
    }

    public static String toMors(String st){
        String morse="";

        for (int i = 0; i < st.length(); i++) {
            char c =st.charAt(i);

            char charUpper = Character.toUpperCase(c);
            String morseCode= alphaMorse.get(Character.toString(charUpper));
            //System.out.println(morseCode);
            morse=  morse+morseCode;

        }
        return morse;
    }
}