Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

unknown problem

Hi experts,
Can someone please tell me what I'm doing wrong here.. I wrote this code

static public doctor FindDoctor(String searchname)
 {
   doctor found=null;
   
   for ( int i = 0; i < getnumofdoc(); i++ )
   {  
     if (darray[i].getname()== searchname)
          {
            found=darray[i];
          }    
   }
   
 return found;  
 }


then I tried testing it with this

String j="jack";

     if(FindDoctor(j)!=null)
     {
       System.out.println("found");
     }
     else System.out.println("not found jack");


when I ran it I kept getting "not found jack" even whenn it was supposed to be returning "found"
where is the problem?

thanks
0
Samooramad
Asked:
Samooramad
  • 6
  • 5
  • 5
3 Solutions
 
CEHJCommented:
>>if (darray[i].getname()== searchname)

should be

if (darray[i].getname().equals(searchname))
0
 
CEHJCommented:
Incidentally, your accessor methods should be 'getName' and your variable probably 'searchName'
0
 
CEHJCommented:
Your class name should be 'Doctor' and your method name 'findDoctor'
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SamooramadAuthor Commented:
that did it... could you explain why it didn't work?

why should I change them
0
 
sudhakar_koundinyaCommented:
And also take care that, once it is found then break the loop. that increases the performance


if (darray[i].getname()== searchname)
          {
            found=darray[i];
              break;

          }    
0
 
sudhakar_koundinyaCommented:
Simple answer is
== is to match the objects but not the object contents
0
 
sudhakar_koundinyaCommented:
>>if (darray[i].getname()== searchname)

should be if (darray[i].getname().equals( searchname))
0
 
SamooramadAuthor Commented:
ok that makes sence :)
thank you (both)
0
 
SamooramadAuthor Commented:
>>our class name should be 'Doctor' and your method name 'findDoctor'
would it make any differnce CEHJ?
0
 
sudhakar_koundinyaCommented:
Well, first off, == is a fundamental operator in the language. The result type of the expression is a boolean. For comparing boolean types, it compares the operands for the same truth value. For comparing reference types, it compares the operands for the same reference value (i.e., refer to the same object or are both null). For numeric types, it compares the operands for the same integer value or equivalent floating point values.

In contrast, equals() is an instance method which is fundamentally defined by the java.lang.Object class. This method, by convention, indicates whether the receiver object is "equal to" the passed in object. The base implementation of this method in the Object class checks for reference equality. Other classes, including those you write, may override this method to perform more specialized equivalence testing.

The typical mistake that most people do is in using == to compare two strings when they really should be using the String class's equals() method. From above, you know that the operator will only return "true" when both of the references refer to the same actual object. But, with strings, most uses want to know whether or not the value of the two strings are the same -- since two different String objects may both have the same (or different) values.
0
 
sudhakar_koundinyaCommented:
>>would it make any differnce CEHJ?


This will not make any difference but maintaining the java standards increases the readabilty
0
 
SamooramadAuthor Commented:
wow..thank you sudhakar...that cleared it up for me :)
0
 
CEHJCommented:
You could also do it like this:

static public doctor FindDoctor(String searchname) {
      List doctors = Arrays.asList(darray);
      Collections.sort(doctors);
      int ix = -1;
      return ((ix = Collections.binarySearch(doctors, searchname)) >= 0? (doctor)list.get(ix) : null;
}
0
 
SamooramadAuthor Commented:
you might have to explain that before I could use it CEHJ :)
0
 
sudhakar_koundinyaCommented:
Well
That is good one If we depends on List from beginning otherwise that  just reduces a performance

Reasons.

Object Array need to be converted to List (means all objects will be added to List instance)
List need to be converted to sorted list (objects in list will be shuffeled as sorted)

Regards
Sudhakar
0
 
CEHJCommented:
It will probably decrease performance for small arrays but could increase it for large ones
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now