Compare 2 strings

0

The idea is that you put for example A and I have to pass it to number, more than number is the value of the index of the array where I have the letters, but it does not work.

//Varianles
        String[] letras  =  {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
        int fila = 0;
        String columna = "";
        val1 = false;
            int col1 = -1;
            while (val1 != true) {

                System.out.print("Diga la columna (A-J): ");
                columna = (teclado.next());
                int col  = -1;
                for (int i = 0; i < letras.length; i++) {
                    if (letras[i].equals(columna)) {
                        col = i;
                    }
                }
                if (col1 == -1) {
                    System.out.println("Letra no valida");
                } else if (col1 > 10 || col1 < 0) {
                    System.out.println("Posicion no valida!");
                } else {
                    val1=true;
                }

            }
    
asked by Djdadi43 11.03.2018 в 14:03
source

2 answers

1

You have a confusion in the use of your variables, you have variables declared at the beginning and within the while , which is what has confused you. It also confused me and I had to debug your program.

Here your code fixed and working (I have commented the lines):

import java.util.Scanner;

public class SO {

    public static void main(String[] args) {

        String[] letras = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
        //int fila = 0; Esta variable no la usas en ningún lado
        String columna = "";
        boolean val1 = false; // Aquí falto establecer el tipo de dato
        //int col1 = -1; Por qué tienes esto si también lo tienes dentro del while?
        Scanner teclado = new Scanner(System.in); // Falto esto también

        while (val1 != true) {

            System.out.print("Diga la columna (A-J): ");

            // Aquí tenías esto: (teclado.next());
            // Está mal escrito, lo he cambiado por esto:
            columna = teclado.next();
            int col = -1; // Tienes esto mismo declarado arriba, mejor usa este

            for (int i = 0; i < letras.length; i++) {
                if (letras[i].equals(columna)) {
                    col = i;
                }
            }

            // Aquí tenías otro error, estabas usando una variable equivocada
            // He cambiado la variable col1 (la que tienes declarada al inicio) por col (la que tienes declarada dentro del while
            if (col == -1) {
                System.out.println("Letra no valida");
            } else if (col > 10 || col < 0) {
                System.out.println("Posicion no valida!");
            } else {
                val1 = true;

                // He agregado esto para saber si la letra ha sido encontrada
                System.out.println("Letra encontrada en la posición " + col);
            }

        }

        teclado.close(); // Cerrar Scanner

    }

}

Evidence of its operation:

    
answered by 11.03.2018 / 14:40
source
-1

You should use:

indexOf (int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

    
answered by 11.03.2018 в 14:14