Search Results

Search found 5 results on 1 pages for 'icelated'.

Page 1/1 | 1 

  • find words in a hashset or treeset?

    - by icelated
    I am piping in a file and storing it into a treeset. I am trying to count unique words.. I am placing words that i dont want into a hashset. "a","the", "and" I want to check to see if the file contains those words, before i place them into the TreeSet.. i know i need some sort of if(word == find) ? i just dont know how to do it.. Sorry about formatting. its hard to get it correct after you paste. this is what i have.. import java.util.Scanner; import java.util.ArrayList; import java.util.TreeSet; import java.util.Iterator; import java.util.HashSet; public class Project1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word; String grab; int count = 0; int count2 =0; int count3 =0; int count4 =0; int number; TreeSet<String> a = new TreeSet<String>(); HashSet<String> find = new HashSet<String>(); System.out.println("Project 1\n"); find.add("a"); find.add("and"); find.add("the"); while (sc.hasNext()) { word = sc.next(); word = word.toLowerCase(); for(int i = 0; i < word.length(); i++ ) { if(Character.isDigit(word.charAt(i))) { count3++; } } //if( a.contains("a") ) //|| word.matches("and") || word.matches("the")|| word.contains("$")) //{ // count2++; // } a.add(word); if (word.equals("---")) { break; } } System.out.println("a size"); System.out.println(a.size()); // count = count2 - count; System.out.println("unique words"); System.out.println(a.size() - count2 - count3); System.out.println("\nbye..."); } }

    Read the article

  • search a collection for a specific keyword

    - by icelated
    What i want to do is search a hashset with a keyword.. I have 3 classes... main() Library Item(CD, DVD,Book classes) In library i am trying to do my search of the items in the hashsets.. In Item class is where i have the getKeywords function.. here is the Items class... import java.io.PrintStream; import java.util.Collection; import java.util.*; class Item { private String title; private String [] keywords; public String toString() { String line1 = "title: " + title + "\n" + "keywords: " + Arrays.toString(keywords); return line1; } public void print() { System.out.println(toString()); } public Item() { } public Item(String theTitle, String... theKeyword) { this.title = theTitle; this.keywords = theKeyword; } public String getTitle() { return title; } public String [] getKeywords() { return keywords; } } class CD extends Item { private String artist; private String [] members; // private String [] keywords; private int number; public CD(String theTitle, String theBand, int Snumber, String... keywords) { super(theTitle, keywords); this.artist = theBand; this.number = Snumber; // this.keywords = keywords; } public void addband(String... member) { this.members = member; } public String getArtist() { return artist; } public String [] getMembers() { return members; } // public String [] getKeywords() // { // return keywords; //} public String toString() { return "-Music-" + "\n" + "band: " + artist + "\n" + "# songs: " + number + "\n" + "members: " + Arrays.toString(members) + "\n" + super.toString() // + "keywords: " + Arrays.toString(keywords) + "\n" + "\n" ; } public void print() { System.out.println(toString()); } } class DVD extends Item { private String director; private String [] cast; private int scenes; // private String [] keywords; public DVD(String theTitle, String theDirector, int nScenes, String... keywords) { super(theTitle, keywords); this.director = theDirector; this.scenes = nScenes; // this.keywords = keywords; } public void addmoviecast(String... members) { this.cast = members; } public String [] getCast() { return cast; } public String getDirector() { return director; } // public String [] getKeywords() // { // return keywords; // } public String toString() { return "-Movie-" + "\n" + "director: " + director + "\n" + "# scenes: " + scenes + "\n" + "cast: " + Arrays.toString(cast) + "\n" + super.toString() // + "keywords: " + Arrays.toString(keywords) + "\n" + "\n" ; } public void print() { System.out.println(toString()); } } class Book extends Item { private String author; private int pages; public Book(String theTitle, String theAuthor, int nPages, String... keywords) { super(theTitle, keywords); this.author = theAuthor; this.pages = nPages; // this.keywords = keywords; } public String getAuthor() { return author; } //public String [] getKeywords() // { // return keywords; //} public void print() { System.out.println(toString()); } public String toString() { return "-Book-" + "\n" + "Author: " + author + "\n" + "# pages " + pages + "\n" + super.toString() // + "keywords: " + Arrays.toString(keywords) + "\n" + "\n" ; } } I hope i didnt confuse you? I need help with the itemsForKeyword(String keyword) function.. the first keyword being passed in is "science fiction" and i want to search the keywords in the sets and return the matches.. What am i doing so wrong? Thank you

    Read the article

  • Observer pattern used with decorator pattern

    - by icelated
    I want to make a program that does an order entry system for beverages. ( i will probably do description, cost) I want to use the Decorator pattern and the observer pattern. I made a UML drawing and saved it as a pic for easy viewing. This site wont let me upload as a word doc so i have to upload a pic - i hope its easily viewable.... I need to know if i am doing the UML / design patterns correctly before moving on to the coding part. Beverage is my abstract component class. Espresso, houseblend, darkroast are my concrete subject classes.. I also have a condiment decorator class milk,mocha,soy,whip. would be my observer? because they would be interested in data changes to cost? Now, would the espresso,houseblend etc, be my SUBJECT and the condiments be my observer? My theory is that Cost is a changes and that the condiments need to know the changes? So, subject = esspresso,houseblend,darkroast,etc.. // they hold cost() Observer = milk,mocha,soy,whip? // they hold cost() would be the concrete components and the milk,mocha,soy,whip? would be the decorator! So, following good software engineering practices "design to an interface and not implementation" or "identify things that change from those that dont" would i need a costbehavior interface? If you look at the UML you will see where i am going with this and see if i am implementing observer + Decorator pattern correctly? I think the decorator is correct. since, the pic is not very viewable i will detail the classes here: Beverage class(register observer, remove observer, notify observer, description) these classes are the concrete beverage classes espresso, houseblend,darkroast, decaf(cost,getdescription,setcost,costchanged) interface observer class(update) // cost? interface costbehavior class(cost) // since this changes? condiment decorator class( getdescription) concrete classes that are linked to the 2 interface s and decorator are: milk,mocha,soy,whip(cost,getdescription,update) these are my decorator/ wrapper classes. Thank you.. Is there a way to make this picture bigger?

    Read the article

  • parent process, and a child process..

    - by icelated
    I am trying to write a program that The parent process will take the arguments to main() and send the characters in them one at a time to the child process through a pipe (one call to write for each character). The child process will count the characters sent to it by the parent process and print out the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever. The child should return normally and not have the parent kill the child. Am i counting the arguments right? am i sending the arguments in one at a time, and am i reaping the child? #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #define size = 100; int main(int argc, char *argv[]) { int i, count =0; int c; int fdest[2]; // for pipe pid_t pid; //process IDs char buffer[BUFSIZ]; if (pipe(fdest) < 0) /* attempt to create pipe */ perror( "pipe" ); if ((pid = fork()) < 0) /* attempt to create child / parent process */ { perror( "fork" ); } /* parent process */ else if (pid > 0) { close(fdest[0]); for (i=1; i < argc; ++i) { for (c=0; c < strlen(argv[i]); ++c) { write(fdest[1], &argv[i][c], 1); } } close(fdest[1]); wait(NULL); exit(0); } else { /* child Process */ close(fdest[1]); while (read(fdest[0], &buffer, 1) > 0) { count++; } printf("\nchild: counted %d characters\n", count); } wait(NULL); exit(0); }

    Read the article

  • 47 memory leaks. STL pointers.

    - by icelated
    I have a major amount of memory leaks. I know that the Sets have pointers and i cannot change that! I cannot change anything, but clean up the mess i have... I am creating memory with new in just about every function to add information to the sets. I have a Cd/ DVD/book: super classes of ITEM class and a library class.. In the library class i have 2 functions for cleaning up the sets.. Also, the CD, DVD, book destructors are not being called.. here is my potential leaks.. library.h #pragma once #include <ostream> #include <map> #include <set> #include <string> #include "Item.h" using namespace std; typedef set<Item*> ItemSet; typedef map<string,Item*> ItemMap; typedef map<string,ItemSet*> ItemSetMap; class Library { public: // general functions void addKeywordForItem(const Item* const item, const string& keyword); const ItemSet* itemsForKeyword(const string& keyword) const; void printItem(ostream& out, const Item* const item) const; // book-related functions const Item* addBook(const string& title, const string& author, int const nPages); const ItemSet* booksByAuthor(const string& author) const; const ItemSet* books() const; // music-related functions const Item* addMusicCD(const string& title, const string& band, const int nSongs); void addBandMember(const Item* const musicCD, const string& member); const ItemSet* musicByBand(const string& band) const; const ItemSet* musicByMusician(const string& musician) const; const ItemSet* musicCDs() const; // movie-related functions const Item* addMovieDVD(const string& title, const string& director, const int nScenes); void addCastMember(const Item* const movie, const string& member); const ItemSet* moviesByDirector(const string& director) const; const ItemSet* moviesByActor(const string& actor) const; const ItemSet* movies() const; ~Library(); void Purge(ItemSet &set); void Purge(ItemSetMap &map); }; here is some functions for adding info using new in library. Keep in mind i am cutting out alot of code to keep this post short. library.cpp #include "Library.h" #include "book.h" #include "cd.h" #include "dvd.h" #include <iostream> // general functions ItemSet allBooks; ItemSet allCDS; ItemSet allDVDs; ItemSetMap allBooksByAuthor; ItemSetMap allmoviesByDirector; ItemSetMap allmoviesByActor; ItemSetMap allMusicByBand; ItemSetMap allMusicByMusician; const ItemSet* Library::itemsForKeyword(const string& keyword) const { const StringSet* kw; ItemSet* obj = new ItemSet(); return obj; const Item* Library::addBook(const string& title, const string& author, const int nPages) { ItemSet* obj = new ItemSet(); Book* item = new Book(title,author,nPages); allBooks.insert(item); // add to set of all books obj->insert(item); return item; const Item* Library::addMusicCD(const string& title, const string& band, const int nSongs) { ItemSet* obj = new ItemSet(); CD* item = new CD(title,band,nSongs); return item; void Library::addBandMember(const Item* musicCD, const string& member) { ItemSet* obj = new ItemSet(); (((CD*) musicCD)->addBandMember(member)); obj->insert((CD*) musicCD); here is the library destructor..... Library::~Library() { Purge(allBooks); Purge(allCDS); Purge(allDVDs); Purge(allBooksByAuthor); Purge(allmoviesByDirector); Purge(allmoviesByActor); Purge(allMusicByBand); Purge(allMusicByMusician); } void Library::Purge(ItemSet &set) { for (ItemSet::iterator it = set.begin(); it != set.end(); ++it) delete *it; set.clear(); } void Library::Purge(ItemSetMap &map) { for (ItemSetMap::iterator it = map.begin(); it != map.end(); ++it) delete it->second; map.clear(); } so, basically item, cd, dvd class all have a set like this: typedef set<string> StringSet; class CD : public Item StringSet* music; and i am deleting it like: but those superclasses are not being called.. Item destructor is. CD::~CD() { delete music; } Do, i need a copy constructor? and how do i delete those objects i am creating in the library class? and how can i get the cd,dvd, destructor called? would the addbandmember function located in the library.cpp cause me to have a copy constructor? Any real help you can provide me to help me clean up this mess instead of telling me not to use pointers in my sets i would really appreciate. How can i delete the memory i am creating in those functions? I cannot delete them in the function!!

    Read the article

1