Compare the data of a matrix with a character

0

I'm starting to program in java, novice in this language, I'm doing a cinema seating assignment program, which I'm doing with a matrix of dimension 6, but when comparing the matrices with a character it performs well but I enter || (or) to compare it with another character and I get an error.

int f = 5, c = 5;         String [] [] seat = new String [f] [c];         int option, purchase, acomular;

    for (f=0;f<asiento.length;f++) 
    {
        for (c=0;c<asiento.length;c++) 
        {
            asiento[f][c]= ("L") ;
        }
    }

    for (f=0;f<asiento.length;f++)
    {

        for (c=0;c<asiento.length;c++)

                System.out.print("["+asiento[f][c]+"]");
                System.out.println(" ");
    }
     System.out.println();

    do{

        opcion=Integer.parseInt(JOptionPane.showInputDialog("Menùº:\n 1.Reservar\n 2.Comprar\n 3.Liberar\n 4.Salir\n Ingrese la opciòn deseada:\n"));

    switch(opcion){

    case 1: f=Integer.parseInt(JOptionPane.showInputDialog("Ingrese la fila:"));
            c=Integer.parseInt(JOptionPane.showInputDialog("Ingrese la columna:"));

            if (asiento[f][c]=="L" || "R") (me sale error y ya probado con varios metodos y no da)
            {
                asiento[f][c]= ("V") ;  

                System.out.println("Asientos ");
                System.out.println();   

                for (f=0;f<asiento.length;f++)
                {
                    for (c=0;c<asiento.length;c++)

                            System.out.print("["+asiento[f][c]+"]");
                            System.out.println(" ");
                }           
             }
    break;

I would like to know how you can compare a matrix with two characters.

    
asked by Chavarría Agudelo 09.02.2018 в 16:27
source

1 answer

1

The syntax for logical operator || receives booleans on both sides of the operator.

In the expression:

asiento[f][c]=="L" || "R"

On the left side you have the Boolean result of asiento[f][c]=="L" but on the right side there is a String.

The correction to compile would be:

asiento[f][c]=="L" || asiento[f][c]=="R"

And the correct way to compare would be:

"L".equals(asiento[f][c]) || "R".equals(asiento[f][c])

Objects in java should be compared with the method equals() . Only primitives are compared with ==

    
answered by 09.02.2018 / 16:49
source