Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Advice

Posted on 2004-08-12
12
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 18

Assisted Solution

by:bobbit31
bobbit31 earned 200 total points
ID: 11787687
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
ID: 11787772
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
ID: 11788102
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11788159
Yes, you need to build a dynamic collection
0
 
LVL 4

Accepted Solution

by:
astorer earned 1800 total points
ID: 11791454
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
ID: 11825134
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
 
LVL 4

Assisted Solution

by:astorer
astorer earned 1800 total points
ID: 11825332
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
ID: 11825810
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
ID: 11825956
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
ID: 11826051
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
ID: 11827983
System.out.println(entry.getKey().toString())
0
 
LVL 4

Expert Comment

by:astorer
ID: 11827995
Hey, thanks for the points!
Good Luck
Andrew
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

719 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