Doubt about the IF

Asked

Viewed 284 times

11

Does the IF function only work with Integers or does it also work with String? I was trying to create a program that would ask if you are sure you want to create the password. You can check the code below:

package com.programas.Verificacao2;
import java.util.Scanner;

public class VoceTemCertezaVerifi {
    public static void main(String[] args) {

     int campo1 = 0, campo2 = 0;
     int sim = 0, não = 0;

     String s = "sim";


    Scanner in = new Scanner(System.in);

      System.out.println("Digite Sua Senha:");
      campo1 = in.nextInt();

      System.out.println("Digite novamente sua senha:");
      campo2 = in.nextInt();

      System.out.println("Você tem certeza?");
      s = in.nextLine();

      if(s == sim) {System.out.println("Senha Criada.");}else{
          if(s == não) {System.out.println("Ação Cancelada");
      }


    }
}
}
  • 1

    I think it answers => http://answall.com/q/34562/91

1 answer

16


To compare string, use equals:

String opcao = "sim";

  if(opcao.equals("sim")){
   System.out.println("Senha Criada.");
  else{
    System.out.println("Ação Cancelada");
  }

By code posted, you can change the validation to receive a String, then just compare using the example above:

public static void main (String[] args) {

     int campo1 = 0, campo2 = 0;
     String confirmacao;
     Scanner in = new Scanner(System.in);

      System.out.println("Digite Sua Senha:");
      campo1 = in.nextInt();

      System.out.println("Digite novamente sua senha:");
      campo2 = in.nextInt();

      System.out.println("Você tem certeza?(Digite sim ou nao)");
      confirmacao = in.next();

  if(confirmacao.equals("sim")) {
    System.out.println("Senha Criada.");
    }else{
          System.out.println("Ação Cancelada");
      }
    }

Another alternative is to use switch, so if the person does not type anything and give enter, it is possible to display a different message:

public static void main(String[] args) {

        int campo1 = 0, campo2 = 0;
        String confirmacao;
        Scanner in = new Scanner(System.in);

        System.out.println("Digite Sua Senha:");
        campo1 = in.nextInt();

        System.out.println("Digite novamente sua senha:");
        campo2 = in.nextInt();

        System.out.println("Você tem certeza?(Digite sim ou nao)");
        confirmacao = in.next();

        switch (confirmacao) {
            case "sim":
                System.out.println("Senha Criada.");
                break;
            case "nao":
                System.out.println("Ação Cancelada");
                break;
            default:
                System.out.println("Opção digitada inválida");
        }
  • thanks for the quick reply.

  • @Danielt Dispo :D If the answer has helped clarify your question, do not forget to mark as accepted by clicking on v just below the left counter.

  • I would like to know why the -1. If something was missing in the answer, without knowing what it was, I will not be able to improve.

  • 1

    I must have accidentally clicked on -1, I have already removed, its response was complete

  • @Danielt without a problem :)

  • 1

    I think that there was something yes in the answer, and you can complement it Diego: the main problem of the code in the question is not the comparison with == (which is a valid comparison of string in almost all popular high-level languages) - but rather, the fact that the "yes" and "not"#of the code in the question are unquoted. With this they are treated as variable names, not as text. (I know you know that - I’m just asking you to put in the answer)

  • @jsbueno worse than he treated as int, and had not even noticed, for me, were string variables yes and no. But the suggestion is valid. I’ll edit the answer based on his code.

  • @jsbueno got good now?

  • 1

    :-) now it’s legal.

  • I’ve solved my problem,

Show 5 more comments

Browser other questions tagged

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