Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Advice

i have vector containing a webaddress and set of words along with a count e.g

[www.aaa.co.uk cars=2 bus=1 automobile=8,  www.abbb.co.uk cars=3 bus-10 phones=7 etc etc]


i want to take a 2 websites and compare the words if the words match i want to mulitply them together eg. take the first two website both website contain the word cars mulitply them together get six

any advice would be grateful
0
HomerrSimpson
Asked:
HomerrSimpson
  • 5
  • 3
  • 2
  • +2
3 Solutions
 
bobbit31Commented:
my advice would be to make a class to store this information in:

public class MyWebAddy {
  private String address;
  private int numCars;
  private int numBus;
  private int numAuto;
  private int numPhones;
  // etc...

  public MyWebAddy() {
    this.address = null;
    this.numCars = 0;
    this.numBus = 0;
    this.numAuto = 0;
    this.numPhones = 0;
  }

  public MyWebAddys(String address, int numCars, int numBus, int numAuto, int numPhones) {
    this.address = address;
    this.numCars = numCars;
    this.numBus = numBus;
    this.numAuto = numAuto;
    this.numPhones = numPhones;
  }

  // getters and setters
  public String getAddress() { return address; }
  public void setAddress(String address) { this.address = address; }

  // etc...
}

then in your main class:

Vector v = new Vector();

MyWebAddy m1 = new MyWebAddy("www.aaa.co.uk", 2, 1, 8, 0);
v.add(m1);

MyWebAddy m2 = new MyWebAddy("www.abbb.co.uk", 3, 10, 0, 7);
v.add(m2);

//etc...

if you want to compare the two websites:

MyWebAddy fromVector1 = (MyWebAddy) v.elementAt(0);
MyWebAddy fromVector2 = (MyWebAddy) v.elementAt(1); // obviously, these can be any element in the vector

System.out.println("Number of Cars: " + fromVector1.getNumCars() * fromVector2.getNumCars());
0
 
CEHJCommented:
You can use Set intersections to solve the problem:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html#retainAll(java.util.Collection)

Put the words like 'car' into two Sets
0
 
wsyCommented:
the words can vary it can be anything i just put cars etc as an example would that rule out bobbit31 idea ? thanks for the suggestion though
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CEHJCommented:
Yes, you need to build a dynamic collection
0
 
astorerCommented:
Where the words can very in this way I would use a Map instead of a Vector for each web address.  This effectively gives  name-value pairs.
So you'd have something like:

Map site1 = new HashMap();
site1.put("cars", "2");
site1.put("bus", "1");
Map site2 = new HashMap();
site2.put("cars", "3");
site2.put("phones", "7");

Can then loop through the entries in site1, test whether the key is in site2's Map too.  For example:

Set values = site1.keySet();
for (java.util.Iterator i = values.iterator() ; i.hasNext() ;)
{
  Map.Entry entry = (Map.Entry)i.next();
  Object o = site2.get(entry.getKey);
  if (o != null && entry.getValue() != null)
  {
    // we have the key in site1 and site2 - as Strings though, so would need to convert to ints
    int i1 = Integer.parseInt((String)entry.getValue());
    int i2 = Integer.parseInt((String)o);
    // multiply them together or do whatever you want to do...
  }
}
0
 
HomerrSimpsonAuthor Commented:
hi astorer i m trying out your code and i get ClassCastException pointed at this line
      Map.Entry entry = (Map.Entry)i.next();

any ideas on how to solve it ?
0
 
astorerCommented:
Hi
Sorry, there is a typo there.
   Set values = site1.keySet();
Should be
  Set values = site1.entrySet();

keySet() returns a Set of the keys (!) which are Strings.
entrySet() returns a Set of Map.Entry objects - so we get the key (i.e. the name) and the value too.

HTH
Andrew
0
 
HomerrSimpsonAuthor Commented:
thanks

i ran the code and it doesnt look like strings from site1 are compare to strings in site2 to check if they match before multiplying?

e.g. site1 bus=2       site2 car=2 bus=2

therefore if i have bus=2 in site1 i need to take out the "bus" part and  check it if site2 contains the word

i assumed this could be done by doing this Object value = site1.get(entry.getValue()); but i get a null. Below is code on that gives me a null on Object value = site1.get(entry.getValue()); i also tried String value = (String)site1.get(entry.getValue()); doesnt work either



                               Set values = site1.entrySet();
     
            System.out.println("values " + values);//prints out values [3d=1, curve=2, 2d=1, animation=2]
             for (Iterator i = values.iterator() ; i.hasNext() ;)
             {
                    Map.Entry entry = (Map.Entry) i.next();
                    Object key = site1.get(entry.getKey());
                    Object value = site1.get(entry.getValue());
                                                System.out.println("key " + key + " value " + value);
 
                                                // prints out
                                                key 1 value null
                                                key 2 value null
                                                key 1 value null
                                                key 2 value null
                                  }

0
 
astorerCommented:
No, you've made a mistake there.  
With the iterator you are looking through the keys and values in site1. So, within the loop, the Map.Entry entry contains the key and value of an element in site1.  So,
  entry,getKey() will give you the key
  entry.getValue() will give you the associated value.

What you want to do is find whether the key exists in site2.  Do this with
   Object value2 = site2.get(entry.getKey());
If value2 does not equal null, then you have a match and value2 is the value in site2

So the original code I posted (apart from the typo) is correct.  Repeated below with extra comments.

Set values = site1.entrySet();   // gives all entries in site1
for (java.util.Iterator i = values.iterator() ; i.hasNext() ;)
{
  Map.Entry entry = (Map.Entry)i.next();   // get next entry and cast it
  Object o = site2.get(entry.getKey());    // get the key of the entry in site1 and look it up in site2

  if (o != null && entry.getValue() != null)   // if not null, then we have a match
  {
    // we have the key in site1 and site2 - as Strings though, so would need to convert to ints
    int i1 = Integer.parseInt((String)entry.getValue());   // This is an integer value from site1
    int i2 = Integer.parseInt((String)o);   // this is an integer value from site2

    // multiply them together or do whatever you want to do...

  }
}

cheers
Andrew
0
 
HomerrSimpsonAuthor Commented:
ahhh right i see it now thanks

one last question how do i just print the word i.e just print out bus from site1 and site2 if there a match ?
0
 
astorerCommented:
System.out.println(entry.getKey().toString())
0
 
astorerCommented:
Hey, thanks for the points!
Good Luck
Andrew
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now