Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2007, 11:26 PM
Member
 
Join Date: Nov 2007
Posts: 13
Rep Power: 0
Feng is on a distinguished road
Default comparing
I want to check which words that i read from an input text file (one word per line) do not occur in an array of strings (that I already have in my program). I used the .equals to compare the word to every word of my array. Then when I print (everytime a word does not occur in in the array) I see e.g. 10 times (=number of elements in the array) the phrase ("The word" + fileLine+ "does not occur."). What should I do? When using arrays there is no 'boolean contains ();'

Last edited by Feng; 11-23-2007 at 07:01 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-23-2007, 12:22 AM
Member
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0
m_srikanth is on a distinguished road
Default
If its an array of Strings, why dont you use String comparision. If not post sample code and it will help us to understand your problem and help u.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-23-2007, 09:40 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
Code:
import java.io.*;
import java.util.*;

public class ComparingStrings {
    static String[] words = {
        "green", "blue", "cyan", "magenta", "orange", "black"
    };

    public static void main(String[] args) {
        String path = "wordsToRead.txt";
        StringBuilder sb = new StringBuilder();
        try {
            Scanner scanner = new Scanner(new File(path));
            while(scanner.hasNextLine()) {
                sb.append(scanner.nextLine() + " ");
            }
            scanner.close();
        } catch(FileNotFoundException e) {
            System.out.println("File not found " + e.getMessage());
        }
        String[] fileWords = sb.toString().split("\\s");
        String[] extraWords = getNewWords(fileWords);
        System.out.printf("extra words = %s%n", Arrays.toString(extraWords));
    }

    private static String[] getNewWords(String[] allWords) {
        List<String> newWords = new ArrayList<String>();
        for(int j = 0; j < allWords.length; j++) {
            if(!isInWords(allWords[j]) && !newWords.contains(allWords[j]))
                newWords.add(allWords[j]);
        }
        return newWords.toArray(new String[newWords.size()]);
    }

    private static boolean isInWords(String s) {
        for(int j = 0; j < words.length; j++) {
            if(words[j].equals(s))
                return true;
        }
        return false;
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing Images shaungoater Advanced Java 0 03-17-2008 10:38 AM
Comparing dates Java Tip Java Tips 0 01-28-2008 09:02 AM
Comparing problem mcal New To Java 1 01-24-2008 03:56 AM
Comparing files JavaForums Java Blogs 0 12-31-2007 09:05 PM
Comparing JavaWebFrameworks pegitha Web Frameworks 1 05-18-2007 06:23 PM


All times are GMT +2. The time now is 10:08 PM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org