• Post Reply Bookmark Topic Watch Topic
  • New Topic

compare the contents of tow arrays  RSS feed

 
Masoud shirzay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to compare the content of 2 arrays(children and files)
and i then only want to print thoes elements of "children", which does not
match the elements of "files". here is my code but it wont print anything
i dont know what is wrong..

int k,arr=100;
String sfiles = " ",cfiles=" ";
String missing=" ";

boolean check[]=new boolean[arr];

String children[]=new String[]{"a","b","c","d","e","f"};

String files[]=new String[]{"f","c","e"};


for (k=0;k<children.length;k++)
{
sfiles=children[k];

boolean found=false;
for(int l=0;l<files.length;l++)
{
cfiles=files[l];
if(!children.equals(files[l]))
found=true;
}
if (!found)
missing=sfiles;
System.out.println("missing files "+missing);

could anyone please help me with this issue.
thanks
[ March 20, 2005: Message edited by: Masoud shirzay ]
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ March 20, 2005: Message edited by: Kashif Riaz ]
 
Divya Mehrotra
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ArrayTest
{
public static void main(String[] args)
{
int k,arr=100;
String sfiles = " ",cfiles=" ";
String missing=" ";
String children[]=new String[]{"a","b","c","d","e","f"};
String files[]=new String[]{"f","c","e"};

for (k=0;k<children.length;k++)
{
sfiles=children[k];
boolean found=false;
for(int l=0;l<files.length;l++)
{
cfiles=files[l];
if(sfiles.equals(cfiles))
{
found=true;
break;
}
}
if (!found)
{
missing=sfiles;
System.out.println("missing files "+missing);
}
}
}
}
[ March 21, 2005: Message edited by: Divya Mehrotra ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Masoud,

First of all, you should use UBB CODE tags when you post code on this forum. These will preserve the formatting as in Kashif's post above.

Second, you should learn to think like the computer. One way to do this is to create a table with your variables. Manually go through each line of code and figure out what variables it changes and what values they are set to. In doing so, you should be able to see when a variable is set to a value that you don't expect. This will help you figure out where your problems are in your code.

Keep Coding!

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!