package ejer14_22;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Morse {
String frase="";
String codigo="";
String[] palabras;
char[] uni= {'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[] morse= {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
public void uni(String str) {
String translate="";
for(int i=0; i<str.length(); i++) {
for(int j=0; j<uni.length; j++) {
if (String.valueOf(str.charAt(i)).equalsIgnoreCase(String.valueOf(uni[j])))
translate+=morse[j]+" ";
}
if(str.charAt(i)==' ' ) translate+=" ";
}
System.out.println(translate);
}
public void split() {
codigo=codigo.replaceAll(" ", " ? ");
palabras=codigo.split("\\s");
System.out.print(palabras.length);
System.out.print("palabras");
for (int j=0; j<palabras.length; j++)
mor(palabras[j]);
}
public void mor(String str) {
String translate="";
for(int j=0; j<morse.length; j++) {
if (str.equals(morse[j]))
translate+=uni[j];
}
if(str.equals("?")) translate+=" ";
System.out.print(translate);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Morse obj=new Morse();
Scanner entrada= new Scanner(System.in);
System.out.println("Que desea hacer?:\n"
+ "Traducir a morse(pulse 1): \n"
+ "Traducir a español(pulse 2): ");
String choice=entrada.nextLine();
switch (choice) {
case "1": System.out.println("Introduzca la frase");
obj.frase=entrada.nextLine();
obj.uni(obj.frase);
break;
case "2": System.out.println("Introduzca el código");
Pattern spaces=Pattern.compile("\\{4,}");
// entrada.useDelimiter(spaces);
obj.codigo=entrada.nextLine();
System.out.println (obj.codigo);
obj.split();
break;
}
}
}
Buenas tardes. What the program does is translate a phrase from Spanish to Morse or vice versa. When translating from Spanish to Morse, the nextline() grabs all the text and translates it fine. But conversely, the nextLine() only takes a portion, I think up to a line separator. The truth is, I don't know why in one case it takes all the text and in the other it doesn't. Since it's because of the nextline() I've looked for ways to make it read further but haven't been able to do it. When option two (translate from Morse to Spanish) is pressed, the Morse code must be entered by separating each Morse letter (for example .- .- .- which is AAA by a space and separating the Morse words by 3 spaces, for example . - .- .- .- .- .-, which would be AAA(3 spaces)AA A. It does everything well except read beyond a morse line...
I see two ways here to do what you want:
The first is to read from a file instead of the input, and read the entire file until there are no more letters to read. Letters would be read in a loop until the end of file (
EOF
) is found. Conceptually it looks like this:The second option is the one you mentioned: read using a delimiter that is a symbol of your choice, different from the line break or the carriage return. So you can read directly from the input.
After some thought I found out that the Scanner's next() method scans the next token and if you set the token with the useDelimiter(String pattern) method, you can make the token as long as you want. In my case, at the end of the String I request a "#" for example and it reads me all the input:
Scanner input= new Scanner(System.in).useDelimiter("#"); obj.code=input.next();