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 10 of 10

Thread: Traverse through the one String and find it contains the other String

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Traverse through the one String and find it contains the other String

    HI,
    I have to write a piece of code in which i have String that can contain a word from a dictionary, that String is named 'prefix' and the dictionary word is current_word,How can i traverse through th 'prefix' to find the current_word in it


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Traverse through the one String and find it contains the other String

    What have you tried? Where are you stuck? Have you looked through the String API for useful functions?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    entwicklerin (January 17th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Traverse through the one String and find it contains the other String

    public static String getDictPhoneWord (String prefix) {

    System.out.println(" :: Inside getDictPhoneWord :: prefix = "+prefix);
    String current_word = "";
    String phWordsFromDict = "";
    // loop through the size of dictionary
    for (int n = 0; n < getDictinList.size(); n++) {

    // current word of dictionary
    current_word = getDictinList.get(n);



    if(prefix.contains(current_word))
    phWordsFromDict = prefix;


    }
    return (phWordsFromDict);
    }

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Traverse through the one String and find it contains the other String

    And does that work? If not, what does it do instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Traverse through the one String and find it contains the other String

    it returns phWordsFromDict = "" i.e. an empty String

  7. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Traverse through the one String and find it contains the other String

    Please help me out

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Traverse through the one String and find it contains the other String

    If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're doing. We have no idea what's in your dictionary, no idea what word you're passing in, and no idea which step is failing (you can find that out by using a debugger or simply adding some print statements).

    I suggest you throw together an SSCCE with a dictionary of about 3 words. Also, did you look at the String API? String (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Traverse through the one String and find it contains the other String

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.Vector;


    public class test4 {

    private static String[] LETTERS = { "E", // 0
    "JNQ", // 1
    "RWX", // 2
    "DSY", // ...
    "FT",
    "AM",
    "CIV",
    "BKU",
    "LOP",
    "GHZ" // 9
    };

    public static ArrayList<String> getDictinList = new ArrayList();



    public static Vector readData () {

    String line=""; // line read from the file
    Vector vector = new Vector();

    // Opening of the file
    try{
    FileInputStream fis = new FileInputStream("C:\\Users\\Awes\\Desktop\\diction ary.txt");
    InputStreamReader isr = new InputStreamReader(fis);
    LineNumberReader lnr = new LineNumberReader(isr);

    while(true){
    line = lnr.readLine();

    // detection of EOF
    if (line == null) break;

    // System.out.println("line "+line);
    getDictinList.add ( line);
    }


    }
    catch(IOException e)
    {
    System.err.println ("Unable to read from file");
    System.exit(-1);
    }

    return vector;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    //phoneWords();
    Scanner input=new Scanner(System.in);
    System.out.print("Enter a Telephone Number : ");

    String number = input.next();
    String alter_number = "";


    /* print num */
    System.out.println("Print number :: "+number);
    if (number.contains("-")){
    alter_number = number.replaceAll("-", "");
    System.out.println("Print number after removing dashes :: "+alter_number);
    phoneWords("", alter_number);
    }
    else

    phoneWords("", number);

    // Vector v = readData();
    }


    public static void phoneWords(String prefix, String number)
    {

    String result = "";


    // If number is empty, we have nothing to do. Prefix should contain
    // a complete string of characters for the phone number.
    if (number.length() == 0) {
    System.out.println(" Inside if number.lemgth() == 0 ");

    System.out.println("prefix :: "+prefix);
    System.out.println("Number :: "+number);


    /********************************************* Compare the prefix with the words in dictionary **********************************/
    Vector v = readData();
    result = getDictPhoneWord(prefix);
    System.out.println("result :: "+result);
    /*
    for (int n = 0; n < getDictinList.size(); n++) {


    current_word = getDictinList.get(n);
    //System.out.println("what is inside getDictinList at the n position :: "+getDictinList.get(n));

    //System.out.println(" current_word :: "+current_word);
    curr_word_letters = current_word.substring(0,2);
    //System.out.println(" curr_word_letters :: "+curr_word_letters);

    if (curr_word_letters.equalsIgnoreCase(prefix.substri ng(0, 2)) ){

    System.out.println("the word that starts with the prefix :: "+current_word);

    break;
    }




    }*/

    return ;
    }

    // Get a temporary copy of the string of letters for the current digit.
    String letters = LETTERS[Character.digit(number.charAt(0), 10)];

    System.out.println("letters :: "+letters);
    // Loop through each letter for the current digit.
    for (int i = 0; i < letters.length(); i++) {

    // Build a substring containing all but the first digit.
    String theRest;
    if (number.length() > 1){
    theRest = number.substring(1);

    System.out.println("theRest if length is greater than 1 :: "+theRest);

    }else{
    theRest = "";
    System.out.println("theRest if length is NOT greater than 1 :: "+number);
    }
    // Recursive call. Append the current letter to the prefix and send
    // it along with the remaining digits.
    phoneWords(prefix + letters.charAt(i), theRest);
    }


    }
    public static String getDictPhoneWord (String prefix) {

    System.out.println(" :: Inside getDictPhoneWord :: prefix = "+prefix);
    String current_word = "";
    String phWordsFromDict = "";

    for (int n = 0; n < getDictinList.size(); n++) {


    current_word = getDictinList.get(n);
    //System.out.println("inside getDictinList current_word at the n position :: "+getDictinList.get(n));

    for (int i = 0; i < prefix.length(); i++){

    System.out.println("Outside if current_word.charAt(i) :: "+current_word.charAt(i) +" prefix.charAt(i) :: "+ prefix.charAt(i));

    if( current_word.charAt(i) == prefix.charAt(i)){
    System.out.println("inside if current_word.charAt(i) :: "+current_word.charAt(i) +" prefix.charAt(i) :: "+ prefix.charAt(i));
    }

    }
    //break;
    //return (phWordsFromDict);
    break;
    }

    return (phWordsFromDict);
    }


    }

  10. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Traverse through the one String and find it contains the other String

    This is my code,Dictionary is a txt document that contain:
    an
    blau
    Bo"
    Boot
    bo"s
    da
    Fee
    fern
    Fest
    fort
    je
    jemand
    mir
    Mix
    Mixer
    Name
    neu
    o"d
    Ort
    so
    Tor
    Torf
    Wasser

  11. #10
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Traverse through the one String and find it contains the other String

    if one enters a phone number it should give the corresponding words from dictionary

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 08:27 AM
  2. [SOLVED] How to find if a sting contains another string?
    By deathpk in forum Java Theory & Questions
    Replies: 2
    Last Post: October 16th, 2011, 08:14 PM
  3. How to find a a certain part in string
    By redzero36 in forum Java Theory & Questions
    Replies: 7
    Last Post: July 19th, 2011, 07:39 PM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  5. [SOLVED] find the position of the field separator in the String---need help ASAP
    By rajesh.mv in forum Java Theory & Questions
    Replies: 6
    Last Post: August 17th, 2009, 11:33 AM