Question and answer with if and Else in Java

Asked

Viewed 3,228 times

5

No matter what I say, both "yes" and "yes" just show the ELSE. Where am I going wrong?

package saudacao;
import java.util.*;
public class Saudacao {
    public static void main(String[] args) {
        System.out.println("Ola, bom dia, voce esta bem hoje?");
        Scanner sdc_recebe = new Scanner(System.in);
        String sdc_armazena = sdc_recebe.nextLine();
        if (sdc_armazena == "Sim") {
            System.out.println("Que bom!!!");
        } else {
            System.out.println("Que pena!!!");
        }
    }
}
  • You’re writing "Yes" or "Yes"?

  • does not matter, either Yes or yes only displays ELSE

  • Hi, Fye, thank by voting positively and if so put this kind of remarks as a comment instead of in the body of the question.

2 answers

6


Use the function String.html#equals, the operator == is used to compare references.

See the difference:

String a = new String("foo");
String b = new String("foo");

System.out.println(a == b);      // False
System.out.println(a.equals(b)); // True 

Your code can go like this:

import java.util.*;

class Saudacao 
{
    public static void main(String[] args) 
    {
        System.out.println("Ola, bom dia, voce esta bem hoje?");

        Scanner sdc_recebe = new Scanner(System.in);
        String sdc_armazena = sdc_recebe.nextLine();

        if (sdc_armazena.equals("Sim")) 
        {
           System.out.println("Que bom!!!");
        } 
        else 
        {
           System.out.println("Que pena!!!");
        }
    }
}

See DEMO

To compare without differentiating between lower and upper case letters, use the function String.html#equalsIgnoreCase:

if (sdc_armazena.equalsIgnoreCase("Sim")) // Sim, sIM, SIM, sim...
{
   System.out.println("Que bom!!!");
} 
else 
{
   System.out.println("Que pena!!!");
} 

To compare integers, String.html#equals can also be used:

Integer I = 10;

if (I.equals(10))
{
   System.out.println("Igual!");
}
else
{
   System.out.println("Valores diferentes!");
}
  • in the case used more for INT that would be the == ?

  • this, as if 1 is equal to 1 XD

4

It could be so:

System.out.println("Ola, bom dia, voce esta bem hoje?");
Scanner sdc_recebe = new Scanner(System.in);
String sdc_armazena = sdc_recebe.nextLine();
if ("sim".equals(sdc_armazena.toLowerCase()) ||
    "aham".equals(sdc_armazena.toLowerCase())) {
     System.out.println("Que bom!!!");
} else {
     System.out.println("Que pena!!!");
}

If you don’t write anything, you don’t make a mistake else.

  • thank you all, it worked perfectly, for me to add positive response options that would be the OR I would have to do other Ifs? or I can put it together?

  • @fyeflourigh, I don’t quite understand ? how ?

  • i want to be able to answer "Yes" OR "Yes" OR "Aham", that is, I want to put more positive answers that the user can answer

  • Ready using toLowerCase() in sdc_stores and compared to "yes" in minuscule any way you type being yes or yes or Sim or etc will pass! @fyeflourigh and use || being OR Aham or yes or yes etc!!! I edited the question

  • ata, este e perfeito, this already took half of the work, but what I really look for and something like the following link, but this is in ALG and in the case wanted the same thing in JAVA http://likb.in/view/77526fab

  • Okay! I get it, in Java this is how you do... as in my answer! Have more questions? @fyeflourigh

  • thank you very much, so in case the OR and the double pipe || am very grateful even, good night

  • This @fyeflourigh !!! in java || is OR or or OR

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.