I want to exit the do while loop when the user press the enter button, but I can't exit the do while loop.
The program asks for two words (pal1 and pal2) and shows which of them is the shortest one and show shows how many characters it has. If the words have the same length it orders them alphabetically. It also shows which of the words asked in the loop is the shortest one and should leave the loop when the string is void or null.
import java.util.Locale;
import java.util.Scanner;
/**
* The program asks for two words (pal1 and pal2) and shows which of them is the shortest one and show shows how many characters it has. If the words have the same length it orders them alphabetically. It also shows which of the words asked in the loop is the shortest one and should leave the loop when the string is void or null.
*
* @author (your name)
* @version (a version number or a date)
*/
public class P5
{
public static void main (String[] args){
Scanner tec = new Scanner (System.in).useLocale(Locale.US);
String pal1, pal2, palMasCorta="", palMasCortaAux;
int longitudpal1, longitudpal2, longitudPalMasCorta=2000000000, longitudPalMasCortaAux;
do{
System.out.println("Escribe una palabra");
pal1 = tec.next();
longitudpal1= pal1.length();
System.out.println("Escribe otra palabra");
pal2 = tec.next();
longitudpal2=pal2.length();
palMasCortaAux=palMasCorta;
longitudPalMasCortaAux=longitudPalMasCorta;
if (longitudPalMasCorta>longitudPalMasCortaAux)
palMasCorta=palMasCortaAux;
if (longitudpal1>longitudpal2){
palMasCorta = pal2;
longitudPalMasCorta = longitudpal2;
System.out.println("El número de caracteres de "+pal2+ " es "+longitudpal2);
}
else
if (longitudpal1 == longitudpal2){
if (pal1.compareTo(pal2) < 0){
System.out.println("El número de caracteres de "+pal1+ " es "+longitudpal1);
palMasCorta=pal1;
}
else{
if (pal1.compareTo(pal2) > 0){
System.out.println("El número de caracteres de "+pal2+ " es "+longitudpal2);
palMasCorta=pal2;
}
else{
System.out.println("Has escrito la misma palabra, "+pal1+", y su longitud es " +longitudpal1);
palMasCorta=pal1;
}
}
}
else{
palMasCorta = pal1;
longitudPalMasCorta = longitudpal1;
System.out.println("El número de caracteres de "+pal1+ " es "+longitudpal1);
}
}while (pal1.length() == 0 || pal2.length() == 0);
System.out.println("La palabra más corta es "+palMasCorta);
}
}