Solved

unknown problem

Posted on 2004-07-31
16
208 Views
Last Modified: 2012-05-05
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
Comment
Question by:Samooramad
  • 6
  • 5
  • 5
16 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 11684161
>>if (darray[i].getname()== searchname)

should be

if (darray[i].getname().equals(searchname))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11684172
Incidentally, your accessor methods should be 'getName' and your variable probably 'searchName'
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11684178
Your class name should be 'Doctor' and your method name 'findDoctor'
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Samooramad
ID: 11684191
that did it... could you explain why it didn't work?

why should I change them
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 250 total points
ID: 11684196
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
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 250 total points
ID: 11684204
Simple answer is
== is to match the objects but not the object contents
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11684218
>>if (darray[i].getname()== searchname)

should be if (darray[i].getname().equals( searchname))
0
 

Author Comment

by:Samooramad
ID: 11684220
ok that makes sence :)
thank you (both)
0
 

Author Comment

by:Samooramad
ID: 11684235
>>our class name should be 'Doctor' and your method name 'findDoctor'
would it make any differnce CEHJ?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11684239
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11684242
>>would it make any differnce CEHJ?


This will not make any difference but maintaining the java standards increases the readabilty
0
 

Author Comment

by:Samooramad
ID: 11684246
wow..thank you sudhakar...that cleared it up for me :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11684248
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
 

Author Comment

by:Samooramad
ID: 11684255
you might have to explain that before I could use it CEHJ :)
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11684274
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11684288
It will probably decrease performance for small arrays but could increase it for large ones
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example issue 6 46
jsp login check 12 43
String array comparison 4 38
Crystal Reports Licensing Questions 4 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question