gpt4 book ai didi

java.lang.StringIndexOutOfBoundsException : String index out of range Error - Morse to English Java Code

转载 作者:行者123 更新时间:2023-12-02 12:22:13 27 4
gpt4 key购买 nike

我正在编写摩尔斯电码到英语的 java 代码(如下),其中 |莫尔斯电码代表字母和数字之间的空格,莫尔斯电码的空格代表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 仅适用于 i(当第一个字符不是空格时。帮助???

最佳答案

i==0时,您在morseBuilder.charAt(i - 1)上失败了

关于java.lang.StringIndexOutOfBoundsException : String index out of range Error - Morse to English Java Code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45684061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com