我正在研究莫尔斯电码到英语Java代码(如下),其中|摩尔斯(Morse)中的空格代表字母和数字之间的空格,摩尔斯(Morse)中的空格代表2个字母或数字之间的空格。例如,“成为” =“---- | -...”。在莫尔斯。
// Import Scanner.
import java.util.Scanner;
public class Project1_szhu1249322
{
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.println("Would you like to translate 'Morse Code' to English, or 'English' to Morse code? (Enter 'Morse Code' or 'English'.)");
String unit1 = input.nextLine();
System.out.println("Enter a string of " + unit1 + " characters (for English, numbers and letters, only): ");
String amountUnit1 = input.nextLine();
if (unit1.equals("Morse Code"))
toEnglish(amountUnit1);
else if (unit1.equals("English"))
toMorseCode(amountUnit1);
else
System.out.println("Invalid data. Enter 'Morse Code' or 'English' without the single quotes.");
}
public static void toMorseCode(String english)
{
// Declare variables, arrays, and strings.
int i = 0;
int l = english.length();
int i2 = 2 * i;
String[] lowerAlphabet = {"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[] upperAlphabet = {"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[] morseCode = {".-", "-...", "-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----"};
morseCode[i] = lowerAlphabet[i];
StringBuilder morseBuilder = new StringBuilder();
// for loops and if statements for result.
for (i = 0; i < l; i++)
{
i2 = 2 * i;
if (english.charAt(i) != ' ')
morseBuilder.append(morseCode[i2]);
else
morseBuilder.append('|');
if (morseBuilder.charAt(i - 1) != ' ' && morseBuilder.charAt(i + 1) != 0)
morseBuilder.append(' ');
}
morseCode[i] = upperAlphabet[i];
// for loops and if statements for result.
for (i = 0; i < l; i++)
{
i2 = 2 * i;
if (english.charAt(i) != ' ')
morseBuilder.append(morseCode[i2]);
else
morseBuilder.append('|');
if (morseBuilder.charAt(i - 1) != ' ' && morseBuilder.charAt(i + 1) != 0)
morseBuilder.append(' ');
}
// Display results.
System.out.println("The corresponding Morse code is " + morseBuilder + ".");
}
public static void toEnglish(String morse)
{
// Declare variables, arrays, and strings.
int i = 0;
int l = morse.length();
int i2 = i / 2;
String[] lowerAlphabet = {"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[] upperAlphabet = {"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[] morseCode = {".-", "-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----"};
morseCode[i] = upperAlphabet[i];
String english;
StringBuilder englishBuilder = new StringBuilder();
// for loops and if statements for result.
for (i = 0; i < l; i++)
{
i2 = i / 2;
if (morse.charAt(i) == '|')
{
englishBuilder.append(' ');
}
else
englishBuilder.append(morseCode[i2]);
}
morseCode[i] = lowerAlphabet[i];
// for loops and if statements for result.
for (i = 0; i < l; i++)
{
i2 = i / 2;
if (morse.charAt(i) == '|')
{
englishBuilder.append(' ');
}
else
englishBuilder.append(morseCode[i2]);
}
// Display results.
System.out.println("The corresponding English is " + englishBuilder + ".");
}
}
我收到此错误:
Would you like to translate 'Morse Code' to English, or 'English' to Morse code? (Enter 'Morse Code' or 'English'.)
English
Enter a string of English characters (for English, numbers and letters, only):
to be
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:237)
at java.lang.StringBuilder.charAt(StringBuilder.java:76)
at Project1_szhu1249322.toMorseCode(Project1_szhu1249322.java:42)
at Project1_szhu1249322.main(Project1_szhu1249322.java:16)
我不明白为什么它会给我这个错误信息。
另外(问题2),我应该使用什么i2值? i / 2仅适用于我(当第一个字符不是空格时。帮助????
最佳答案
当morseBuilder.charAt(i - 1)
时,您在i==0
上失败