arrays - Incorrect output for Java in a Morse to English program -
here morse code english , vice versa assignment criteria:
- in morse code, | stands blank space.
- in morse code, blank space stands new letter or digit.
my code compiling correctly, output (after running it) incorrect.
// 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 == "morse code") toenglish(amountunit1); else if (unit1 == "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 + "."); } }
my output this:
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): invalid data. enter 'morse code' or 'english' without single quotes.
it not going right if-else branch. instead of going unit1 == "english", goes last else invalid data.
also, for loop in toenglish method, how should set i2? need / 2, if first character not blank space.
when creating translated string should use following instead of charat() =
doesn't allow assignment:
stringbuilder bob = new stringbuilder(); (...){ if (...){ bob.append(character); } } string english = bob.tostring();
also i2
equal 0
.
there number of other small bugs in here. carry on debugging , solve them all.
Comments
Post a Comment