Solved

Advice

Posted on 2004-08-12
12
239 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JAVA API design with micro service cloud in mind 1 111
Problem to Alipay 10 94
learn programming 8 94
Java array sort 10 62
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

734 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