Solved

Advice

Posted on 2004-08-12
12
234 Views
Last Modified: 2010-03-31
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
Comment
Question by:HomerrSimpson
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 18

Assisted Solution

by:bobbit31
bobbit31 earned 50 total points
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 

Expert Comment

by:wsy
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Yes, you need to build a dynamic collection
0
 
LVL 4

Accepted Solution

by:
astorer earned 450 total points
Comment Utility
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
 

Author Comment

by:HomerrSimpson
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Assisted Solution

by:astorer
astorer earned 450 total points
Comment Utility
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
 

Author Comment

by:HomerrSimpson
Comment Utility
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
 
LVL 4

Expert Comment

by:astorer
Comment Utility
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
 

Author Comment

by:HomerrSimpson
Comment Utility
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
 
LVL 4

Expert Comment

by:astorer
Comment Utility
System.out.println(entry.getKey().toString())
0
 
LVL 4

Expert Comment

by:astorer
Comment Utility
Hey, thanks for the points!
Good Luck
Andrew
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now