我几乎成功完成了这项任务,但并未完全实现。所以我需要的是例如我给DOG这个词,程序会查看一个文本文件并返回DOG和GOD,即只能通过给出的赔率生成的单词。我的代码给了我所有包含' D' O' O'和' G'。我的代码是这样的:
public class JavaReadTextFile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ReadFile rf = new ReadFile();
String filename = "/Users/Elton/Desktop/OSWI.txt";
String wordinput;
String wordarray[] = new String[1];
System.out.println("Input Characters: ");
wordinput = input.nextLine();
wordarray[0] = wordinput;
System.out.println(wordinput.length());
try {
String[] lines = rf.readLines(filename);
for (String line : lines) {
if (line.matches(wordarray[0] + ".*")) {
System.out.println(line);
}
}
} catch (IOException e) {
System.out.println("Unable to create " + filename + ": " + e.getMessage());
}
}
}
-----然后我有:
public class ReadFile {
String [] cName = new String [100];
public String[] readLines(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null ) {
cName[0] = line.split(" ")[0];
lines.add(cName[0]);
}
bufferedReader.close();
return lines.toArray(new String[lines.size()]);
}
}
答案 0 :(得分:1)
我可以看到你能够从文件中读取单词。其余的工作很简单。算法将是这样的
sort inputWord
对您从文件中读取的字词进行排序
如果两个单词相同,则将其添加到某个列表中。
以上是上述算法的简单演示,您可以根据需要对其进行修改。
public class App {
static String sortString (String str) {
char []chars = str.toCharArray();
sort(chars);
return new String(chars);
}
public static void main(String... args) {
String inputWord = "DoG";
String readWord = "God";
inputWord = inputWord.toUpperCase();
readWord = readWord.toUpperCase();
inputWord = sortString(inputWord);
readWord = sortString(readWord);
if(inputWord.equalsIgnoreCase(readWord)) {
System.out.println(readWord);// you can add it to your list
}
}
}
答案 1 :(得分:0)
这可以帮助您调整代码。
import java.util.regex.* ;
public class Find_Dogs_And_Gods
{
public static void main(String []args)
{
String line = "2ldoghmDoggod" ;
Pattern p = Pattern.compile("[d,D,g,G][o,O][d,D,g,G]") ;
Matcher m = p.matcher(line) ;
while(m.find() )
{
System.out.println( m.group() ) ;
}
}
}
答案 2 :(得分:0)
如果我理解正确,你想显示一个单词的所有字谜吗?
更改方法readLines()
以返回ArrayList
而不是数组。
ArrayList<String> readLines(String fname) {
File file = new File(fname);
ArrayList<String> list = null;
try {
Scanner scanner = new Scanner(file);
list = new ArrayList<String>();
while (scanner.hasNext()) {
String currentWord = scanner.next();
if (!currentWord.isEmpty()) {
list.add(currentWord);
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list;
}
使用此函数,输入参数字典是从readLines返回的ArrayList。该函数使用两个字母如“DOG”和“GOD”在排序时都是相等的字符串(即“DGO”等于“DGO”)这一事实
public ArrayList<String> getAnagrams(String word, ArrayList<String> dictionary) {
if(word == null || dictionary == null) {
return null;
}
ArrayList<String> anagrams = new ArrayList<String>();
char[] sortedChars = word.toCharArray();
Arrays.sort(sortedChars);
for(String item : dictionary) {
char[] sortedDictionaryItem = item.toCharArray();
Arrays.sort(sortedDictionaryItem);
if(Arrays.equals(sortedChars, sortedDictionaryItem)) {
anagrams.add(item);
}
}
return anagrams;
}
如果您不喜欢我建议的更改,您还可以执行以下操作。在您执行的循环中:
if (line.matches(wordarray[0] + ".*")) {
System.out.println(line);
}
您可以检查两个字符串是否是彼此的排列:
if (isPermutation(line, wordarray[0]) {
System.out.println(line);
}
通过添加这两个功能:
String sortString(String s) {
char[] chars = s.toCharArray();
java.util.Arrays.sort(chars);
return new String(chars);
}
boolean isPermutation(String s1, String s2) {
if(s1.length() != s2.length()) {
return false;
}
s1 = sortString(s1);
s2 = sortString(s2);
return (s1.compareTo(s2) == 0);
}