Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

compare the contents of tow arrays

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ March 20, 2005: Message edited by: Kashif Riaz ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic