Searching a Vector

Searching a vector problems

Page 1 of 1

7 Replies - 836 Views - Last Post: 05 May 2010 - 03:09 AM Rate Topic: -----

#1 Guest_Guest*


Reputation:

Searching a Vector

Posted 04 May 2010 - 09:35 PM

Hello ALl

Latly in a programming assigment which reuires us to construct a program which counts up how many times a specific word appears in a string of them. What i am having trouble with is how to get it to search through the Node file to find the word and then return that word along with its count.

Heres my code
i don't know much about how to program (i'm a noob) so yea
Thanks

package Model;

public class Index {
	
	private Index vector = null;
	
	public Index(String words){
	}
	   
	public void addNode(String word){
	}
	   
	public boolean isIn(String word){
		return true;
	}
	   
	private Node findWord(String word){
	   
	   	Node MyNode = null;
	   
	   	return MyNode;
	}
	   
	public void incCount(String word){
	}
	   
	public void addWord(String word){
	}
	   
	public Integer getCount(String word){
	   
		return this.count; (its meant to go here but i don't know how to fix it)
	}
	   
	public String toString(){
		return "me";
	}
}


Edited by Dogstopper: :code:

This post has been edited by Dogstopper: 04 May 2010 - 09:40 PM


Is This A Good Question/Topic? 0

Replies To: Searching a Vector

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Searching a Vector

Posted 04 May 2010 - 09:39 PM

If it's not too advanced, you should use a Map. If that String exists as a key in the Map, increase the integer value by 1, otherwise, add it to the Map. Look in the API for more information:
http://java.sun.com/...a/util/Map.html
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Pieman*


Reputation:

Re: Searching a Vector

Posted 04 May 2010 - 09:45 PM

Lately in our IPT class we got an assigment which is due next week in which we need to count up how many times a certain word appears. I need help in the part to actually find how many times the words appear. If you look at the code and look at the brackets you'll see what i mean.

This is my code
Thanks enomosly

package Model;

public class Index {
	
	private Index vector = null;
	
	public Index(String words){
	}
	   
	public void addNode(String word){
	}
	   
	public boolean isIn(String word){
		return true;
	}
	   
	private Node findWord(String word){
	   
	   	Node MyNode = null;
	   
	   	return MyNode;
	}
	   
	public void incCount(String word){
	}
	   
	public void addWord(String word){
	}
	   
	public Integer getCount(String word){
	   
		return count;  (this is where its meant to break off and get the count by searching through the vector which i don't know how to do)
	}
	   
	public String toString(){
		return "me";
	}
}



This post has been edited by pbl: 04 May 2010 - 09:53 PM
Reason for edit:: Code tags added for a newbiw

Was This Post Helpful? 0

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Searching a Vector

Posted 04 May 2010 - 09:47 PM

As Dogstopper mentionned a HashMap will do the job

Here is code I writtent today
I ahave several HashMap whih key=Integer value=Integer

	// to add an element to the hashmap. Add 1 to the count if already there else just create it
	private void addToHash(HashMap<Integer, Integer> hash, int id) {
		if(hash.containsKey(id)) {
			int nb = (Integer) hash.get(id);
			++nb;
			hash.put(id, nb);
		}
		else {
			hash.put(id, 1);
		}

	}


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Searching a Vector

Posted 04 May 2010 - 09:50 PM

Please respect this forum rules
:code:

And Give your topic a descriptive title, NOT "Help" or "Plz Help Me" or "I'm a noob"

Topic title edited
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Searching a Vector

Posted 05 May 2010 - 03:04 AM

Topics merged. Please don't create duplicate topics!
Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Searching a Vector

Posted 05 May 2010 - 03:05 AM

well, start by creating a new node, and set it to the vector's head.
iterate the vector using that node until the node gets null. if you find a node with the same word you look for, increase the count.
at the end of the iteration, return the count.

don't forget to declare count as:
int count = 0;

and why is the return type is Integer? no need. you can set it to return an int value, as:
public int getCount(String word){


**oh, sorry, didn't see it was a duplicate post, I posted before they were merged.
anyways, if my suggestion is offtopic, or has already made by other poster, please ignore it :)

This post has been edited by japanir: 05 May 2010 - 03:08 AM

Was This Post Helpful? 1
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Searching a Vector

Posted 05 May 2010 - 03:09 AM

Ah...I see. I was thinking that that method was different...Very nice japanir!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1