Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2007, 12:26 AM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
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 08:01 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-23-2007, 01:22 AM
Member
 
Join Date: Nov 2007
Posts: 8
m_srikanth is on a distinguished road
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, 10:40 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 11:38 AM
Comparing dates Java Tip Java Tips 0 01-28-2008 10:02 AM
Comparing problem mcal New To Java 1 01-24-2008 04:56 AM
Comparing files JavaForums Java Blogs 0 12-31-2007 10:05 PM
Comparing JavaWebFrameworks pegitha Web Frameworks 1 05-18-2007 07:23 PM


All times are GMT +3. The time now is 10:44 AM.


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