简体   繁体   English

System.out.println不打印数组数据

[英]System.out.println not printing array data

I am still very new to Java and have been trying to get a morse code translator to work. 我仍然是Java的新手,并一直试图让莫尔斯电码转换器工作。 I overcame the first problem with various errors, but now the program compiles but will not print the results of the translation. 我克服了各种错误的第一个问题,但现在程序编译但不会打印翻译结果。 Any help would be appreciated. 任何帮助,将不胜感激。

import java.util.Scanner;

public class MorseCode
{
    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("To convert English to Morse Code, type M. To convert Morse Code to English, type E.");

        String cType = Input.nextLine();

        String type = cType.toLowerCase();

        if(type == "m")
        {
            String eng;
            System.out.println("Please enter the English text to be translated.");
            eng = Input.nextLine();
            EToM(eng);
        }
        else
        {
            String morse;
            System.out.println("Please enter the Morse code text to be translated, with multiple words seperated by a |.");
            morse = Input.nextLine();
            MToE(morse);
        }
    }
    public static void EToM(String eng)
    {
        String EToMList[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".--", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "----.", "-----", "|"};

        String alphabet = "abcdefghijklmnopqrstuvwxyz123456789 ";
        String translation[] = new String[eng.length()];

        for(int x = 0; x < eng.length(); x++)
        {
            for(int y = 0; y < alphabet.length(); y++)
            {
                if(eng.charAt(x) == alphabet.charAt(y))
                {
                    translation[x] = EToMList[y];
                    System.out.println("test");
                }
            }
        }

        System.out.println("Your translated message is:");

        for(int z = 0; z < eng.length(); z++)
        {
            System.out.println(translation[z]); 
        }
    }

    public static void MToE(String morse)
    {

    }
}       

Your problem is 你的问题是

if(type == "m")

use 采用

if("m".equals(type))

Your if-else will go to else because you are comparing String references and not String values. 你的if-else将转到else因为你正在比较String引用而不是String值。 The else calls the MToE method which is empty. else调用空的MToE方法。 Read this: How Do I compare Strings in Java 阅读: 如何比较Java中的字符串

When checking strings for equality in Java, always use the equals method on the String class. 在Java中检查字符串是否相等时,请始终在String类上使用equals方法。 Changing the following: 更改以下内容:

if(type == "m")

to

if(type.equals("m"))

makes the English to Morse code translation output. 使英语到莫尔斯代码翻译输出。

I made this modification and ran it successfully just now. 我做了这个修改,刚才成功运行了。

use This 用这个

  if(type.equalsIgnoreCase("m")
  {

  }

You can use the deepToString method from Java to print out your array. 您可以使用Java中的deepToString方法打印出您的数组。

Documentation Link: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#deepToString(java.lang.Object[]) 文档链接: http//docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#deepToString(java.lang.Object [])

Here is a code sample: 这是一个代码示例:

System.out.println("Your translated message is:" + java.util.Arrays.deepToString(translation));    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM