所以這是我的代碼的一部分。如果我爲preTranslation字符串輸入了「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()在「決定」字符串(和比較它(「莫爾斯」),所以你不必告訴用戶「注意帽子「,只是一個念頭! –