Results 1 to 4 of 4
- 12-13-2008, 08:47 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 9
- Rep Power
- 0
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.???
- 12-13-2008, 09:45 PM #2
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
- 12-13-2008, 10:43 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 9
- Rep Power
- 0
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>
- 12-14-2008, 01:52 PM #4
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 aabove the if( string.equals) and see what the Last is actually getting.Java Code:System.out.println(Last);
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
-
Counting Duplicate Variables in an Array
By Npcomplete in forum New To JavaReplies: 2Last Post: 10-24-2008, 07:33 PM -
Counting the number of columns in a 2D array,
By KalEl in forum New To JavaReplies: 9Last Post: 10-21-2008, 05:27 AM -
Error: incompatible types, found: int required: boolean
By silvia in forum New To JavaReplies: 6Last Post: 10-08-2008, 08:09 AM -
Array of different data types?
By venkatteshb in forum New To JavaReplies: 1Last Post: 08-27-2008, 05:42 PM -
problem with scanner class:incompatible types
By fred in forum New To JavaReplies: 1Last Post: 07-20-2007, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks