Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: java pharse searcher

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java pharse searcher

    Hi all,
    Im trying to create program what is seraching all pharse combination from left to right, from txt file were at least 4 chars. Its working when i have in *.txt file only one word, but im trying to get it work with mutiple words in same time.
    forexample: I have string quality: and all combinations are: uali, quali, ality, qualit, uality, lity, ualit, quality, qual, alit. If i have strings quality milk utput should be ---> uali, quali, ality, qualit, uality, lity, ualit, quality, qual, alit,milk etc


     
     
    package test;
    import java.io.*;
    import java.util.*;
     
    final public class testSub
    {
        public static void main(String...args) throws FileNotFoundException
        {
            read();
     
        }
     
        private static void read() throws FileNotFoundException {
            String string, sub;
     
             ArrayList<String>al=new ArrayList<String>();
           int i = 0, c, length,count = 0;
     
                    // Create Scanner object
            Scanner in = new Scanner(new File("words.txt"));
     
     
         // Read a string
          string  = in.nextLine();
     
     
     
          length = string.length();
     
     
     
     
     
            for(c=0;c<length;c++)
            {
     
     
                for(i=1;i<=length-c;i++)
                {
     
     
                    sub = string.substring(c,c+i);
                    //System.out.println(sub);
                    al.add(sub);
     
     
                }
            }
     
     
            HashSet hs = new HashSet();
            hs.addAll(al);
            al.clear();
            al.addAll(hs);
     
            for(String str:al)
            {
     
                if(str.length()>3){
     
     
                System.out.println(str);
                }
     
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java pharse searcher

    Can you explain what your problem is?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java pharse searcher

    problem is i want to improve this code to process all words in *.txt file.Fornow this code is working correctly when *.txt contains only one word. Forexample:quality. If i put in text file mutiple words: quality woman milk etc. Its not working. I know i need to use split and arrays put i failed .It sould process independently all atleast 4 chars lenghts phrases per word.
    Example:quality: all combinations are: uali, quali, ality, qualit, uality, lity, ualit, quality, qual, alit

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java pharse searcher

    Is the problem getting the words one at a time from the input?

    What problem did you have using the split() method?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java pharse searcher

    output output should be

    uali
    quali
    ality
    qualit
    uality
    lity
    ualit
    quality
    qual
    alit
    milk

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java pharse searcher

    Can you explain the steps the code needs to take to get the desired output?
    Something like this:
    begin loop
    get next word
    print all substrings of word
    end loop
    If you don't understand my answer, don't ignore it, ask a question.