Results 1 to 3 of 3
  1. #1
    karunabdc is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default searching char array with another char array for full word matches

    Hi all,

    I am very new to Java (Semester 1 of university) and I am having trouble trying to figure out an algorithm to search a char array with another char array to find word matches.

    I have tried using two for-loops and finding word matches like so:

    Java Code:
        public static void main(String[] args) {
            String textToSearch = "the quick brown fox jumped over the lazy dog quick quick";
            String searchTerm = "quick";
            int charMatches =0;
            char[] textToSearchArray = textToSearch.toLowerCase().toCharArray();
            char[] searchTermArray = searchTerm.toLowerCase().toCharArray();
    
            for (int i=0; i < textToSearchArray.length; i++)
            {
                for (int j=0; j < searchTermArray.length; j++)
                {
                    if (textToSearchArray[i]==searchTermArray[j])
                        charMatches ++;
                }
            }
    
            System.out.println("The number of word matches are: "+(charMatches/searchTermArray.length));
        }
    If I run this code, it returns a correct number of word matches (3)......but if I change the textToSearch string to "hello hello hello" and the searchTerm string to "hello", it tells me that there are 4 matches?

    I think it is because the "l"s are counted twice and I am pretty sure this algorithm of mine is not a really good way to go about the task.


    DISCLAIMER: yes, this is for a homework assignment, but I have tried Googling for a solution, reading books and working it out on paper... I really want to understand how to do this and any help would be greatly appreciated .

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Why are you fannying about with char arrays? Just use the methods of the String class.
    Java Code:
    String words = "one two three";
    String search = "two";
    System.out.println(words.contains(search));
    System.out.println(words.indexOf(search));

  3. #3
    karunabdc is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    I have to use char arrays,

    the assignment is split into 3 parts,
    in parts one and two, I am free to use string methods, which I did,
    however in part 3 I have to write an algorithm that splits the strings into char arrays and then find word matches by comparing them

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 12:32 PM
  2. array to single char
    By rfviki in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 03:58 PM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 10:01 PM
  4. array of char
    By sinisab in forum New To Java
    Replies: 9
    Last Post: 01-05-2010, 10:48 AM
  5. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 12:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •