Solved

Advice

Posted on 2004-08-12
12
238 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 86

Expert Comment

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

Accepted Solution

by:
astorer earned 450 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 450 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 64
MySqlDump not dumping triggers 1 43
Adding multiple JVM environments to RedHat 6 7 49
eclipse console opening separately 2 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

820 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