java - How to get a program to continue reading input after if statements are fulfilled (Morse Translator) -
before begin, novice programmer having been doing day.
how program continue reading input after input has been fulfilled? below code, morse code english translator trying make, when input morse, example .-, gives me correct output, a. when combine morse letters, example .--..., should ab, else statement activates. should do?
import java.util.scanner;
public class morsetranslator {
public static void main(string[] args) { system.out.println("please enter morse code wish translate."); scanner sc =new scanner(system.in); string morse = sc.next(); if (morse.equals(" ")) { system.out.print(" "); } if (morse.equals(".-")){ system.out.print("a"); } if (morse.equals("-...")){ system.out.print("b"); } if (morse.equals("-.-.")){ system.out.print("c"); } if (morse.equals("-..")){ system.out.print("d"); } if (morse.equals(".")){ system.out.print("e"); } if (morse.equals("..-.")){ system.out.print("f"); } else system.out.println("please input morse code."); }
}
string.equals() compares complete strings, .--... never equals .- , need 'look for' inside morse string, using string.indexof()
if(morse.indexof(".-")!=-1){ system.out.print("a"); //need more magic here }
now need 'substract' or takeout 2 characters morse string, , repeat searching loop.
if(morse.indexof(".-")!=-1){ system.out.print("a"); morse=morse.substring(morse.indexof(".-")+2); // 2 morse characters continue; //your hypothetical loop } if(morse.indexof("-...")!=-1){ system.out.print("b"); morse=morse.substring(morse.indexof("-...")+4); // 4 morse characters continue; //your hypothetical loop } ...
don't forget loop until there's no more data process
Comments
Post a Comment