Comparing 2 arrays 1and 2 dimensional

Hi,

I have a 2 dimensional array:

public static String [][] numbers = {{"four", "four4"}, {"five", "five5"};

And I have another one dimensional array:

public static String [] site= {"one", "two", "three", "four"};

What I want to do is iterate over the array site and if the same word exists in the first column of numbers add the result to the sb variable which is of the type StringBuilder.

Here is my effort so far but it doesn't work..

public String checkArray()

{

StringBuilder sb = new StringBuilder(20);

for(int i = 0; i < site.length; i++)

{

for(int j = 0; j < numbers.length; j++)
{
if(site[i].equals(numbers[i][0]))
{

sb.append([i]);
}

}
return sb;
}

}
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

``````// Try changing this...
if(site[i].equals(numbers[i][0]))

// to this...
if(site[i].equals(numbers[j][0]))
``````
0
Commented:
return sb;
should be out of second first loop
``````for(int i = 0; i < site.length; i++)
{
for(int j = 0; j < numbers.length; j++)
{
if(site[i].equals(numbers[j][0]))
{
sb.append([i]);
}

}
}
return sb;
``````
0
Commented:
>> second first loop

read as " first for loop"
0
Author Commented:
Ok thanks that makes sense but when I try and compile I get an error: on this line

newPage.append([i]) - illegal  start of expression

Any ideas?

0
Commented:
This is likely due to a misplaced brace or a missing semicolon, etc. that precedes the given statement.
0
Commented:
>> newPage.append([i]) - illegal  start of expression

Complete code will help in finding the problem...
0
Author Commented:
Here is the complete code I think it is because the sb.append expects a string and i is not a string, any ideas?

public static String [][] numbers = {{"four", "four4"}, {"five", "five5"};
public static String [] site= {"one", "two", "three", "four"};

public String checkArray()

{

StringBuilder sb = new StringBuilder(20);

for(int i = 0; i < site.length; i++)
{
for(int j = 0; j < numbers.length; j++)
{
if(site[i].equals(numbers[j][0]))
{
sb.append([i]);
}

}
}
return sb;

}

0
Commented:
StringBuilder append function all types of primitives and strings..

You need to change.. sb.append([i]);

to

sb.append(site[i]);
0
Author Commented:
Thanks, that fixed that problem now I get:

if(site[i].equals(numbers[j][0])) - array required but java but java.lang.string found
0
Commented:
Still couple of problems..

no closing brace at..

>> public static String [][] numbers = {{"four", "four4"}, {"five", "five5"};
to
public static String [][] numbers = {{"four", "four4"}, {"five", "five5"}};

Return type of function is string..

So change
>> return sb;
to
return sb.toString();
0
Author Commented:
Thanks, still get an error on that line, I'll take a another look:

if(site[i].equals(numbers[j][0])) - array required but java but java.lang.string found
0
Commented:
It works fine for me..
basically there is no problem in that line..

Can u post the complete code again.. now.. ?
0
Author Commented:
Ok here is the full code:
public static String [][] numbers = {{"four", "four4"}, {"five", "five5"}};
public String page = "the index is here"

public static String[] compare()
{
String[] comp = page.split (" ");
return comp;
}

public String checkArray()

{

StringBuilder sb = new StringBuilder(20);
String[] site = page();

for(int i = 0; i < site.length; i++)
{
for(int j = 0; j < numbers.length; j++)
{
if(site[i].equals(numbers[j][0]))
{
sb.append(site[i]);
}

}
}
return sb;

}

0
Commented:
The following line.. is wrong..

String[] site = page();

There is no page function..

I think U want the line should be changed to..

String[] site = compare();
0
Author Commented:
Sorry that was a typo that line is String[] site = compare();
0
Commented:
I dont no, how are you copying the code.. here.. :) , Better you copy and paste

There are still couple of problems..

>> public String page = "the index is here"

1) Does not end with ";"
2) also should be declared static, because, this is being accessed in a static function.

0
Author Commented:
Yes maybe better to copy and paste, it is now as per my previous post and the modifications you mentioned.  Still get the same error.
0
Commented:
What is your compiler version
0
Commented:
I am not getting any compiler error with the following code in jdk1.5 and jdk 1.6
``````public class Test
{
public static String [][] numbers = {{"four", "four4"}, {"five", "five5"}};
public static String page = "the index is here";

public static String[] compare()
{
String[] comp = page.split (" ");
return comp;
}

public String checkArray()

{

StringBuilder sb = new StringBuilder(20);
String[] site = compare();

for(int i = 0; i < site.length; i++)
{
for(int j = 0; j < numbers.length; j++)
{
if(site[i].equals(numbers[j][0]))
{
sb.append(site[i]);
}

}
}
return sb.toString();

}
}
``````
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks for all the hard work on this.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.