Java - Problem with if [duplicate]

1

I have the following code

package Condicionales;
import java.util.Scanner;

public class PruebaJava {  
    public static void main(String[] args) {
        /*float numero1;
        float numero2;
        float total;
        */Scanner lector = new Scanner(System.in);/*
        System.out.println("Ingrese un valor numerico");
        System.out.println("RECUERDA QUE PARA USAR DECIMALES USA ','");
        numero1 = lector.nextFloat();

        System.out.println("Ingrese un valor numerico X2");
        numero2 = lector.nextFloat();*/

        System.out.println("Escoja el tipo de operacion a realizar");
        System.out.println("1. Suma");
        System.out.println("2. Resta");
        System.out.println("3. Multiplicacion");
        System.out.println("4. Division");

        System.out.println("Escriba el numero o tipo de operacion");
        String tipoOperacion = lector.next();

        if (tipoOperacion == "Suma" || tipoOperacion == "suma") {
            System.out.println("Suma!!");
        }

        System.out.println("... " + tipoOperacion);
    }
}

In the if part, if I enter "Sum" the condition is not fulfilled, I thought that possibly in the variable typeOperacion something else was being saved, but no, it is exactly "Sum" and the if does not return true

  

Keep in mind that if I add the value to the variable from the code, it works correctly, but when the user is allowed to enter the value, the if does not return true.

    
asked by Máxima Alekz 07.03.2017 в 06:40
source

2 answers

1

test with nextLine() and to compare strings .equals()

is used

solving your case:

String tipoOperacion = lector.nextLine();

That's when a single word is entered in the line. but what happens if you enter several, for example "sum of two", in that case you must convert to an array with the function split

String [] palabras=tipoOperacion.split(" ");

tipoOperacion=palabras[0];

was missing declare the variable typeOperacion, you must condition if the reader has an element with .hasNext()

import java.util.Scanner;

public class PruebaJava {  
    public static void main(String[] args) {
        /*float numero1;
        float numero2;
        float total;
        */Scanner lector = new Scanner(System.in);/*
        System.out.println("Ingrese un valor numerico");
        System.out.println("RECUERDA QUE PARA USAR DECIMALES USA ','");
        numero1 = lector.nextFloat();

        System.out.println("Ingrese un valor numerico X2");
        numero2 = lector.nextFloat();*/

        System.out.println("Escoja el tipo de operacion a realizar");
        System.out.println("1. Suma");
        System.out.println("2. Resta");
        System.out.println("3. Multiplicacion");
        System.out.println("4. Division");
        String tipoOperacion="";
        System.out.println("Escriba el numero o tipo de operacion");

      while (lector.hasNext())
      {
         tipoOperacion=lector.nextLine();
      }

        if (tipoOperacion.equals("Suma") || tipoOperacion.equals("suma")) {
            System.out.println("Suma!!");
        }

        System.out.println("... " + tipoOperacion);
    }
}

do not use a lot of scanner, I show you an alternative BufferedReader

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PruebaJava {  
    public static void main(String[] args) throws Throwable {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        String tipoOperacion="";

        System.out.println("Escoja el tipo de operacion a realizar");
        System.out.println("1. Suma");
        System.out.println("2. Resta");
        System.out.println("3. Multiplicacion");
        System.out.println("4. Division");

        System.out.println("Escriba el numero o tipo de operacion");
        if((line = in.readLine()) != null) {
            tipoOperacion=line;
        }

        if (tipoOperacion.equals("Suma")|| tipoOperacion.equals("suma")) {
            System.out.println("Suma!!");
        }

        System.out.println("... " + tipoOperacion);
    }
}
    
answered by 07.03.2017 / 06:50
source
1

The condition is not evaluated because you are reading from the keyboard in an infinite loop. Try entering the equals condition ("Sum") into the loop.

import java.util.Scanner;

public class PruebaJava {  
    public static void main(String[] args) {
        /*float numero1;
        float numero2;
        float total;
        */Scanner lector = new Scanner(System.in);/*
        System.out.println("Ingrese un valor numerico");
        System.out.println("RECUERDA QUE PARA USAR DECIMALES USA ','");
        numero1 = lector.nextFloat();

        System.out.println("Ingrese un valor numerico X2");
        numero2 = lector.nextFloat();*/

        System.out.println("Escoja el tipo de operacion a realizar");
        System.out.println("1. Suma");
        System.out.println("2. Resta");
        System.out.println("3. Multiplicacion");
        System.out.println("4. Division");
        String tipoOperacion="";
        System.out.println("Escriba el numero o tipo de operacion");

      while (lector.hasNext())
      {
         tipoOperacion=lector.next();
         if (tipoOperacion.contentEquals("Suma") || tipoOperacion.contentEquals("suma")) {
            System.out.println("Suma!!");
        }
      }

        System.out.println("... " + tipoOperacion);
    }
}
    
answered by 07.03.2017 в 13:34