Results 1 to 4 of 4
  1. #1
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default Incompatible types??? Counting through an array of Strings

    having a problem counting through an array of Strings. I am trying to count how many employee have the last name Smith.

    final int EMPLOYEE=5;
    Employee[] id= new Employee[EMPLOYEE];
    id[0]=new Employee("john", "Smith");
    id[1]=new Employee("Mary", "King");
    id[2]=new Employee("Josh", "Smith");

    My Code:



    for (int index=0; index < id.length; index++)
    {
    String Last;
    int count=0;
    Last=id[index].getLastName();
    if (Last=="Smith")
    {
    count+=id[index];
    System.out.println("There are" + count + "with the last name Smith");
    }


    }

    I get an error of incompatible types. int and String.???

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    common first code error, use String.equals("Smith");

    and just do ++count or count++
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    ookie833 is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default

    I changed the code to:

    if (Last.equals("Smith"))
    {
    count++;
    }


    but now it just says 1 person was found with the last name smith instead of 2 people. Not sure why>

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Not sure how the Employee class is coded but the code count+=id[index]; assigns a ( an ) EMPLOYEE to an int, you show code that changed that. You should get a null pointer exception on index++ when it gets past the three employees and arrives at the fourth position in the array, which does not have an employee new'd to that position. Sometimes this type of error is obscured by fancy tools, and in any event is best resolved by tediously walking through the code again and again till you find it.

    Place a
    Java Code:
    System.out.println(Last);
    above the if( string.equals) and see what the Last is actually getting.

    Do this for testing.

    Probably do println(count); also - see what is actually being produced.

    This seems tedious, after about two of these you will really like the idea.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 07:33 PM
  2. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 05:27 AM
  3. Replies: 6
    Last Post: 10-08-2008, 08:09 AM
  4. Array of different data types?
    By venkatteshb in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 05:42 PM
  5. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:02 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
  •