[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Comparing 2 arrays 1and 2 dimensional

Posted on 2008-02-10
20
Medium Priority
?
175 Views
Last Modified: 2010-04-21
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;
           }
         
         
       }
0
Comment
Question by:sjsharp80
  • 10
  • 8
  • 2
20 Comments
 
LVL 8

Expert Comment

by:Thejaka
ID: 20861058

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

Open in new window

0
 
LVL 23

Expert Comment

by:cmalakar
ID: 20861131
  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;

Open in new window

0
 
LVL 23

Expert Comment

by:cmalakar
ID: 20861138
>> second first loop

read as " first for loop"
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:sjsharp80
ID: 20861377
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
 
LVL 8

Expert Comment

by:Thejaka
ID: 20861410
This is likely due to a misplaced brace or a missing semicolon, etc. that precedes the given statement.
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 20863929
>> newPage.append([i]) - illegal  start of expression

Complete code will help in finding the problem...
0
 

Author Comment

by:sjsharp80
ID: 20864857
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
 
LVL 23

Expert Comment

by:cmalakar
ID: 20864901
StringBuilder append function all types of primitives and strings..

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

to

sb.append(site[i]);
0
 

Author Comment

by:sjsharp80
ID: 20864960
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
 
LVL 23

Expert Comment

by:cmalakar
ID: 20865018
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 Comment

by:sjsharp80
ID: 20865040
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
 
LVL 23

Expert Comment

by:cmalakar
ID: 20865151
It works fine for me..
basically there is no problem in that line..

Can u post the complete code again.. now.. ?
0
 

Author Comment

by:sjsharp80
ID: 20865209
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
 
LVL 23

Expert Comment

by:cmalakar
ID: 20865282
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 Comment

by:sjsharp80
ID: 20865293
Sorry that was a typo that line is String[] site = compare();
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 20865315
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 Comment

by:sjsharp80
ID: 20865364
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
 
LVL 23

Expert Comment

by:cmalakar
ID: 20865376
What is your compiler version
0
 
LVL 23

Accepted Solution

by:
cmalakar earned 2000 total points
ID: 20865387
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();
 
}
}

Open in new window

0
 

Author Closing Comment

by:sjsharp80
ID: 31429603
Thanks for all the hard work on this.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question