java.lang.StringIndexOutOfBoundsException: String index out of range Error - Morse to English Java Code -
i working on morse code english java code (below) in | in morse stands blank space between letters , numbers, , blank space in morse stands in between 2 letters or digits. ex., "to be" = "- --- | -... ." in morse.
// 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 translate 'morse code' english, or 'english' morse code? (enter 'morse code' or 'english'.)"); string unit1 = input.nextline(); system.out.println("enter string of " + unit1 + " characters (for english, numbers , 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 single quotes."); } public static void tomorsecode(string english) { // declare variables, arrays, , strings. int = 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(); // loops , if statements result. (i = 0; < 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]; // loops , if statements result. (i = 0; < 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 " + morsebuilder + "."); } public static void toenglish(string morse) { // declare variables, arrays, , strings. int = 0; int l = morse.length(); int i2 = / 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(); // loops , if statements result. (i = 0; < l; i++) { i2 = / 2; if (morse.charat(i) == '|') { englishbuilder.append(' '); } else englishbuilder.append(morsecode[i2]); } morsecode[i] = loweralphabet[i]; // loops , if statements result. (i = 0; < l; i++) { i2 = / 2; if (morse.charat(i) == '|') { englishbuilder.append(' '); } else englishbuilder.append(morsecode[i2]); } // display results. system.out.println("the corresponding english " + englishbuilder + "."); } }
i getting error:
would translate 'morse code' english, or 'english' morse code? (enter 'morse code' or 'english'.) english enter string of english characters (for english, numbers , letters, only): exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: -1 @ java.lang.abstractstringbuilder.charat(abstractstringbuilder.java:237) @ java.lang.stringbuilder.charat(stringbuilder.java:76) @ project1_szhu1249322.tomorsecode(project1_szhu1249322.java:42) @ project1_szhu1249322.main(project1_szhu1249322.java:16)
i not understand why giving me error message.
also (question 2), i2 value should use? / 2 works (when first character not blank space. help???
you failing on morsebuilder.charat(i - 1)
when i==0
Comments
Post a Comment