Problem when comparing one to one letters of two strings

3

Hello the problem says like this:

  

Create a class whose main method executes a letter-by-letter comparison using two-word equals using loops.

     

For example, if the words are “avispa” and “ave” the program should result in:
  Letter 1 equal in the two words? True .
  Letter 2 equal in the two words? True
  Letter 3 equal in the two words? False
  Letter 4 equal in the two words? La palabra 2 no tiene letra 4
  Letter 5 equal in the two words? La palabra 2 no tiene letra 5
  Letter 6 equal in the two words? La palabra 2 no tiene letra 6 .

I try it this way (I clarify that it must be object oriented):

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Introduce palabra 1: ");
    Entrega662 cadena1=new Entrega662();
    System.out.println("Ingrese palabra 2: ");
    Entrega662 cadena2=new Entrega662();

    if(cadena1.getLargo()>cadena2.getLargo()){
        boolean iguales;
        for(int x=0;x<cadena1.getLargo();x++){
            System.out.println("¿Letra "+(x+1)+" igual en las dos palabras");
               if(cadena1.getLetra(x).equals(cadena2.getLetra(x))){
                   iguales=true;
                   System.out.println(iguales);
               }else{
                   iguales=false;
                   System.out.println(iguales);
               }
               if(x>cadena2.getLargo()){
                   System.out.println("Cadena 2 no tiene letra "+(x+1));
               }

        }
    }

Look at the result you give me:

Introduce palabra 1: 
Avispa
Ingrese palabra 2: 
Ave
¿Letra 1 igual en las dos palabras
true
¿Letra 2 igual en las dos palabras
true
¿Letra 3 igual en las dos palabras
true
¿Letra 4 igual en las dos palabras
true
¿Letra 5 igual en las dos palabras
true
Cadena 2 no tiene letra 5
¿Letra 6 igual en las dos palabras
true
Cadena 2 no tiene letra 6
    
asked by Jesu 30.03.2017 в 01:20
source

1 answer

1

Modify the code a bit:

public static void main(String[] args) {
        System.out.println("Introduce palabra 1: ");
        Entrega662 cadena1 = new Entrega662();
        System.out.println("Ingrese palabra 2: ");
        Entrega662 cadena2 = new Entrega662();

        if (cadena1.getLargo() > cadena2.getLargo()) {
            boolean iguales = false;
            for (int x = 0; x < cadena1.getLargo(); x++) {

                if (x < cadena2.getLargo()) {
                    System.out.println("¿Letra " + (x + 1) + " igual en las dos palabras?");
                    if (cadena1.getLetra(x).equals(cadena2.getLetra(x))) {
                        iguales = true;
                        System.out.println(iguales);
                    } else if (iguales == true) {
                        iguales = false;
                        System.out.println(iguales);
                    }
                } else {
                    System.out.println("Cadena 2 no tiene letra " + (x + 1));

                }
            }
        }
    }

What I did was add an if to see if the variable x was still smaller than the string2 since if I kept doing the comparison I would mark a StringIndexOutOfBoundsException because the string 2 is smaller, if I was smaller than the comparison if it does not send the message that it no longer contains the lyrics.

Console output:

Introduce palabra 1: 
avispa
Ingrese palabra 2: 
ave
¿Letra 1 igual en las dos palabras?
true
¿Letra 2 igual en las dos palabras?
true
¿Letra 3 igual en las dos palabras?
false
Cadena 2 no tiene letra 4
Cadena 2 no tiene letra 5
Cadena 2 no tiene letra 6

I hope I can help you.

    
answered by 30.03.2017 в 01:52