Results 1 to 4 of 4

Thread: equal() method

  1. #1
    need_helpp is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default equal() method

    The program should read in the student number, first name and last name from user input. Every time user inputs student number it should call equals() method and check to see if this student number already exist. I don't know how can i call the method in my Test file.
    Student.java

    Java Code:
    public class Student{
    	
    private String firstName;
    private String lastName;
    private String studentNumber;
    private Object getFirstName;
    
       firstName,lastName,studentNumber
         */
    	Student(String fName,String lName,String sNumber) 
    	{
    		firstName=fName;  
    		lastName=lName;  
    		studentNumber=sNumber;
    		
    	} 
    	
    	public Student() 
    	{	
    	
    	}
    	
    	public void setFirstName(String fName)  	{
    	firstName=fName;   
    	} 
    	
    	public String getFirstName()   
    	{
    		return firstName;
    		
    	}
    	
    	public void setLastName(String lName) 
    	{
    	lastName=lName;   
    		
    	} 
    	
    	public String getLastName() 
    	{
    		return lastName;
    		
    	}
    	
    	public void setStudentNumber(String sNumber)  
    	{
    	studentNumber=sNumber;   
    		
    	} 
    	
    	public String getStudentNumber()
    	{
    	return studentNumber;
    		
    	}
    	 
    	public String toString() 
    	{
    		return String.format("%-8s %-9s %-8s\n",firstName,lastName,studentNumber );
    	}//end method toString
    	
    	public boolean equals (Object o) { 
    	       if (o instanceof Student) { 
    	           Student s = (Student) o; 
    	           if (s.getFirstName.equals(firstName))  
    	                   return true; 
    	       } 
    	       return false; 
    	   }    
    
    		  
    }
    StudentTest.java
    Java Code:
    import java.util.Scanner;
    
    public class StudentTest {
    	public static void main( String[] args)
    	{   
    		Student[] students = new Student[2];
    		Scanner inputs = new Scanner(System.in);
    		for(int i = 0 ; i < students.length; i++)
    	    { 
    	       
    	      System.out.println("Please enter Student Number");
    	      String fName = inputs.nextLine(); 
    	      System.out.println("Please enter Student First Name");
    	      String lName = inputs.nextLine(); 
    	      System.out.println("Please enter Student Last Name");
    	      String sNumber = inputs.nextLine(); 
    	      
    	      students[i] = new Student(fName, lName, sNumber);
    
    	       
    	}
    		for(int i = 0 ; i < students.length; i++)
    	    { 
    		System.out.print(students[i].toString());
    	}
    	
    		
    }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by need_helpp View Post
    System.out.println("Please enter Student Number");
    String fName = inputs.nextLine();
    System.out.println("Please enter Student First Name");
    String lName = inputs.nextLine();
    System.out.println("Please enter Student Last Name");
    String sNumber = inputs.nextLine();

    students[i] = new Student(fName, lName, sNumber);

    paah, why do you assign the student number to fName and the first name to lName and the Last Name to sNumber?

    if you read only two students then assign the first student to s1 and the second to s2 and declare your equals method with

    public static boolean equals (Student s1, Student s2)

    so you can call it with Student.equals (s1, s2)

    and the check for equals with

    if (s1.getStudentNumber().equals(s2.getStudentNumber( )))

    but this is a bad implementation, because this code will not work for more then two students. so, i suggest you use a more generic collection than arrays and as already mentioned a better code for more students. a more generic solution could pass a reference to the collection and the new student to your equals-method and then inside the method iterate through the collection to find out if the number already exists. good luck.

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Your Student class looks good, but in your testing class, I would also suggest switching from an array to a more generic collection, possibly a List.

    First plan out what you have to do.....
    1) Read in a Student
    -----a) Construct a Student class containing the read in data.
    2) See if Student was already entered
    -----b) Go through the List of Unique Students already entered. Compare each one to the one that was just entered using the equals() method.
    -----c) If it wasn't already entered, Add it to the Unique Students List.
    3) Loop back to Step 1.


    To answer your main question though, you can call the .equals() method by calling it on the instances of the Students you have created. Example...

    Java Code:
    Student s1 = new Student("Cal", "Robot", "20030903");
    Student s2 = new Student("Tec", "Man", "20948411");
    if (s1.equals(s2)){
         System.out.println("Same");
    }

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    OK. First of all, Object specifies an equals(Object other) method. All it does is look at the internal JVM references to see if they are the same. It is the same as saying "this == other".

    Second, the "right" way to do this is to override the equals() method in your Student class. I assume this is homework, and your instructor expects you to do it this way. Note that equals() is NOT a static method.

    Here is a standard implementation:

    Java Code:
    @Override
    public boolean equals(Object other) {
      // Same object
      if (this == other) {
        return true;
      }
      // If other is null, instanceof returns false
      if (!(other instanceof Student)) {
        return false;
      }
      // Compare this to other using student number, return true or false
     you have to do this yourself
    }

Similar Threads

  1. ComparisonFailure on equal(to the eye) strings
    By staffan in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:57 PM
  2. Problem using equal() method
    By ookie833 in forum New To Java
    Replies: 3
    Last Post: 11-18-2008, 05:19 AM
  3. Entering a while loop with a not equal to string
    By bri1547 in forum New To Java
    Replies: 9
    Last Post: 07-09-2008, 07:10 AM
  4. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 06:21 AM
  5. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 0
    Last Post: 11-18-2007, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •