Another Java Problem

Discussion in 'Tech Discussion' started by AMissingLinguist, Oct 28, 2017.

  1. BnP32yhDT

    BnP32yhDT New Member

    Joined:
    Oct 28, 2017
    Messages:
    2
    Likes Received:
    3
    Reading List:
    Link
    I only registered to reply to you, all these "answers" who are just talking nonsense, while not pointing out the error... shame on you.

    Try Arrays.equals(arr1, arr2) instead of arr1.equals(arr2)

    Next time you can use a debugger to step through your code to find the fault on your own.
     
    kjoke and AMissingLinguist like this.
  2. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    Thank you for giving an answer. I can't seem to do the arr1.equal(arr2) correctly.
     
  3. asdf123

    asdf123 Well-Known Member

    Joined:
    Dec 22, 2016
    Messages:
    293
    Likes Received:
    174
    Reading List:
    Link
    i was gonna say we dont need to see those but he might have messed that part up lol, never know
     
  4. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    Everything else is correct. It's only this part of the code that gets a wrong output.
     
  5. BnP32yhDT

    BnP32yhDT New Member

    Joined:
    Oct 28, 2017
    Messages:
    2
    Likes Received:
    3
    Reading List:
    Link
    Since i cant link directly stack overflow com questions 8777257 equals-vs-arrays-equals-in-java
     
    AMissingLinguist likes this.
  6. TamaSaga

    TamaSaga Well-Known Member

    Joined:
    Oct 11, 2016
    Messages:
    1,726
    Likes Received:
    2,173
    Reading List:
    Link
    So has the problem been solved? If not, I'd like to try my hand.
     
    AMissingLinguist likes this.
  7. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    Problem solved. No need for more answers.
     
  8. Vilidious

    Vilidious Well-Known Member

    Joined:
    Jul 28, 2017
    Messages:
    699
    Likes Received:
    892
    Reading List:
    Link
    By the way, what about null checks? Or are all elements and arrays guaranteed not null?
     
  9. AMissingLinguist

    AMissingLinguist [Not Here][Blank Sect][Nuffian #N]

    Joined:
    May 15, 2016
    Messages:
    2,297
    Likes Received:
    6,383
    Reading List:
    Link
    I've coded in the initial variables already, but the problem's solved. No worries.
     
    Vilidious likes this.
  10. KingOfDreams

    KingOfDreams [Master of Sleepiness]

    Joined:
    Jan 7, 2017
    Messages:
    210
    Likes Received:
    106
    Reading List:
    Link
    PHP:
    public class test {
        
        public static 
    void main(String args[]){
        
    PetRecord test1=new PetRecord();
        
    test1.name="test1";
        
    test1.age=10;
        
    PetRecord test2=new PetRecord();
        
    test2.name="test1";
        
    test2.age=10;
        
    PetRecord test3=new PetRecord();
        
    test3.name="test3";
        
    test3.age=20;
        
        
    System.out.println(test1.compare(test2));
        
    System.out.println(test2.compare(test3));
        }
    }

    class 
    PetRecord{
        
    int age=0;
        
    String name="";
        
    String getName(){
          return 
    name
        };
        
    int getAge(){
          return 
    age;
        };
        
    boolean compare(PetRecord other){
            
    boolean result=false;
            if(
    this.getName().equals(other.getName()) && this.getAge()==other.getAge())
                
    result=true;
            return 
    result;
        }
    };
    it's a bit messy but you get the idea you need to use their functions that you created you cannot use equals on primitives
     
    AMissingLinguist likes this.