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
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?
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);
}
Re: Traverse through the one String and find it contains the other String
And does that work? If not, what does it do instead?
Re: Traverse through the one String and find it contains the other String
it returns phWordsFromDict = "" i.e. an empty String
Re: Traverse through the one String and find it contains the other String
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)
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);
}
}
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
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