Data comparison after nextLine java

Asked

Viewed 71 times

-2

I’m unable to make my verification structure work, I can’t after typing stop, display the message and the program continue the loop.

/* * To change this License header, Choose License Headers in Project Properties. * To change this template file, Choose Tools | Templates * and open the template in the editor. */ package javaapplication4;

/**
 *
 * @author Deny
 */
import java.util.Scanner;

public class JavaApplication4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       String resposta;
       String r;
       String respostaVerificadora;
       String[] camgirls = new String[50000];
       Scanner scan = new Scanner(System.in);

       //cadastro de cam girls
       for(int c=0; c < 50000; c++) {
           System.out.println("Digite o nome da camgirl de numero: "+c);
          //ISSO AQUI NAO TA FUNCIONANDO EU DIGITO PARE AQUI
           resposta = scan.next();
           //PARA DAR IGUAL AQUI
           respostaVerificadora = "pare";
     //E NA HORA DE VERIFICAR QUE OS DOIS SAO IGUAIS, O JAVA NAO RECONHECE E NAO ENTRA NO IF
           if(resposta == respostaVerificadora ) {
               System.out.println("parei");
               break;
           }

       }
        System.out.println("PROGRAMA FINALIZADO");
    }

}

1 answer

0


Java has no operator overhead when you compare objects using ==, Java compares the reference of these objects. Even if the value of the strings are equal, the references are different, to compare the values you need to use the equals method.

resposta.equals(respostaVerificadora)

I also suggest you try to learn how to use the Java List, because creating arrays of 50000 variables is absurdly inefficient.

Browser other questions tagged

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