이것은 내 코드의 일부입니다. preTranslation String에 "1111"을 입력하면 true가 출력되지만 false를 출력합니다. 나는 equal()이 두 값을 비교할 수는 없지만 나는 다른 방법을 모른다. 어떤 도움이 필요합니까?문자열 값과 배열 값의 동일성
import javax.swing.JOptionPane;
import java.util.Arrays;
public class test3
{
public static void main (String [] args)
{
String s1 = "Morse";
// Decide whether Morse code or English
String decide = JOptionPane.showInputDialog("Enter 'English' for Morse to English code translation and 'Morse' for English to Morse code translation. Pay attention to Caps.");
// Enter String & decide whether to convert to Morse or English
String preTranslation = JOptionPane.showInputDialog("Enter the words you wish to translate.");
if (decide.equals(s1))
toMorse(preTranslation);
else
toEnglish(preTranslation);
}
// Translate to Morse
public static void toMorse(String preTrans)
{
// Initialize english letters
char[] english = new char[3];
// Initialize english numbers
english[0] = 1;
english[1] = 2;
english[2] = 3;
// Initialize morse characters
String[] morse = {".","-", ".-"};
// Replace spaces with |
String phraseWithDelimiter = preTrans.replace(" ", "|");
String[] translation = new String[phraseWithDelimiter.length()];
System.out.println(phraseWithDelimiter.substring(0, 1).equals(english[0])); // Should print out to be true
}
}
여기에 단지 의견을 달아주세요. "결정"문자열에 toLower()를 사용하고 ("morse"와 비교하여) 사용자에게 "주의를 기울이지 마십시오."라고 말할 필요가 없습니다. Caps ". 그냥 생각! –