1
Aprender a programar desde cero / Re:Método equals en Java. Diferencia entre igualdad e identidad. Ejemplo CU00662B
« en: 17 de Octubre 2017, 01:37 »
aqui dejo otra solucion
codigo:

codigo:
Código: [Seleccionar]
import java.util.Scanner;
public class ComparadorLetras {
public static void main (String[] args) {
int longitud;
int j = 1;
int menor;
Scanner capt = new Scanner(System.in);
System.out.println("Escriba dos cadenas: \nPrimera cadena:");
String texto1 = capt.nextLine();
System.out.println("Segunda cadena: ");
String texto2 = capt.nextLine();
System.out.println("");
if (texto1.length() > texto2.length()) { //establece la longitud de la cadena mayor para usarla como rango del for
longitud = texto1.length();
menor = 2;
}
else {longitud = texto2.length();
menor = 1;
}
for (int i=0; i <= longitud-1;i++) {
try {
System.out.println("Letra no. " + i + " igual en 2 palabras ----> " + texto1.substring(i,j).equals(texto2.substring(i,j)));
j = j + 1;
} catch (Exception e) {//establece si existe un error imprima el siguiente texto
System.out.println("La palabra " + menor + " no tiene letra " + (i+1));
}
}
}
}