I need to make the igName() method return if they are the same or different. I made the comparison with double and function, but with String the if does not compare

Asked

Viewed 33 times

0

Computer

package Exercício06_Aula8;

import Objeto.Objeto;

public class Computador {


    private double ck;
    private double hd;
    private String nome;

    public void setCk(double ck) {
        this.ck=ck;
    }

    public double getCk() {
        return this.ck;
    }
    //////////////////
    public void setHd(double hd) {
        this.hd=hd;
    }
    public double getHd() {
        return this.hd;
    }
    //////////////////
    public void setNome(String nome) {
        this.nome=nome;
    }
    public String getNome() {
        return this.nome;
    }
    ///////////////////

    //////////////////////////////////////////////////////////////////

    public void print() {
        System.out.println("Nome == "+getNome());   
        System.out.println("Tamanho do hd "+ getHd());
        System.out.println("Frequencia do clock "+getCk());

    }
    /// compara clock===
    public boolean igCk(Computador c2) {
        boolean resp; 
        if (this.getCk()==c2.getCk()) {
            resp=true;

        }
        else {
            resp=false;
        }
        return resp;
    }
    /// compara hd===
    public boolean igHd(Computador c2) {
        boolean resp;
        if(this.getHd()==c2.getHd()) {
            resp=true;
        }
        else {
            resp=false;
        }
        return resp;
    }

    //// compara nomes=== 
    public boolean igNome(Computador c2) {
        boolean resp;
        if (this.getNome()==c2.getNome()) {
            resp=true;
        }
        else {
            resp=false;
        }
    return resp;
    }



    }

Computerized

package Exercício06_Aula8;
import java.util.Scanner;

import Objeto.Objeto;
public class ComputadorTeste {

    public static void main(String[] args) {
        Scanner ler = new Scanner(System.in);
        Computador c1 = new Computador();
        Computador c2 = new Computador();

        System.out.println("Digite o nome ");
        c1.setNome(ler.next());

        System.out.println("Digite a frequencia o clock");
        c1.setCk(ler.nextDouble());

        System.out.println("Digite o tamanho do HD");
        c1.setHd(ler.nextDouble());
        //////////////////////////////////////////
        System.out.println("Segundo Computador === ");

        System.out.println("Digite o nome ");
        c2.setNome(ler.next());

        System.out.println("Digite a frequencia do clock");
        c2.setCk(ler.nextDouble());

        System.out.println("Digite o tamanho do hd ");
        c2.setHd(ler.nextDouble());

        ////metodos ====
        System.out.println("\n"+"Resultado === ");
        c1.print();
        System.out.println("\n"+"Segundo obj==== ");
        c2.print();

        System.out.print("\nSe os valores são iguais ou diferentes == \n");
        ////////// primeira comparação ====
        if(c1.igCk(c2)) {
            System.out.println("\nclocks Iguais "+c1.getCk()+"||"+c2.getCk());

        }
        else {
            System.out.println("\nclocks são diferentes "+c1.getCk()+"||"+c2.getCk());
        }
        /////////Segunda comparação =====

        if (c1.igHd(c2)) {
            System.out.println("\nHDs iguais "+c1.getHd()+"||"+c2.getHd());
        }
        else {
            System.out.println("\nHDs diferentes "+c1.getHd()+"||"+c2.getHd());
        }

        ////// terceira comparação de nome ===
        if(c1.igNome(c2)) {
            System.out.println("\nNomes são iguais ");
        }
        else {
            System.out.println("\nNomes são diferentes");
        }

    }
}
  • Do not use == to compare Strings. See in the question that was marked as duplicate why and how you should then proceed in this case.

1 answer

0

String has a different treatment, experiment using the equals method, this way:

    //// compara nomes=== 
    public boolean igNome(Computador c2) {

       return this.getNome().equals(c2.getNome());

    }
  • Thank you. Would you tell me why the different treatment ? It’s just that I’m studying And I didn’t find the answer anywhere.

  • you can have a look at the answer to that question already asked, follow the link: https://answall.com/questions/3905/como-comparar-strings-em-java

Browser other questions tagged

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