Java Anagrams 从字典文件中读取

发布于 2024-12-13 01:31:20 字数 2159 浏览 0 评论 0原文

我在将字典文件与字谜词进行比较时遇到问题。我在每个地方都放置了一个打印语句,它正确地读取字典文件,并且也正确地计算所有的字谜词,但它不会只计算字典文件中的字谜词。我很确定这是一个非常小的问题,如果有人可以修复它,我们将不胜感激。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; 
import java.util.List;
import java.util.Scanner;

public class Anagram3 
{
  static int size;
  static int count;
  static char[] charArray;
  static char[] words;

public static void main(String[] args) throws IOException 
{
Scanner sc = new Scanner(System.in);
System.out.println("Type the path of the dictionary to read from : ");
String fileName = sc.nextLine();

List<String> dictionary = new ArrayList<String>();

BufferedReader br = null;     

try
{
  br = new  BufferedReader(new FileReader(fileName));
  String word;

  while((word = br.readLine())!=null)
  {
      dictionary.add(word);
  }

}
catch(IOException e)
{
  e.printStackTrace();
}
String[] words = new String[dictionary.size()];
dictionary.toArray(words);

//for( int i = 0; i < words.length; i++ )
//    System.out.println(words[i]);


System.out.println("\nEnter the phrase to scramble: ");
String input = sc.nextLine();
System.out.println();


    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
      charArray[j] = input.charAt(j);
    doAnagram(size);
 }

public static void doAnagram(int newSize) 
{
 int limit;
if (newSize == 1) // if too small, return;
  return;
// for each position,
for (int i = 0; i < newSize; i++) {
  doAnagram(newSize - 1); // anagram remaining
  if (newSize == 2) // if innermost,
    printAnagrams(); 
  rotate(newSize); // rotate word
}
}


public static void rotate(int newSize) 
{
int i;
int position = size - newSize;

char temp = charArray[position];

for (i = position + 1; i < size; i++)
  charArray[i - 1] = charArray[i];

charArray[i - 1] = temp;
}

public static void printAnagrams() 
{
for (int i = 0; i < size; i++)
{    
  //System.out.print(charArray[i]);
    if(charArray[i] == words[i])
    {
        System.out.print(charArray[i]);
    }

 }
 System.out.println();
}
}

I am having trouble comparing my dictionary file to the anagrams. I put a print statement at each and it is reading in the dictionary file correctly and it is also calculating all of the anagrams correctly But it won't calculate only the anagrams from the dictionary file. I'm pretty sure it's something very minor and if someone can fix it it would greatly be appreciated.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList; 
import java.util.List;
import java.util.Scanner;

public class Anagram3 
{
  static int size;
  static int count;
  static char[] charArray;
  static char[] words;

public static void main(String[] args) throws IOException 
{
Scanner sc = new Scanner(System.in);
System.out.println("Type the path of the dictionary to read from : ");
String fileName = sc.nextLine();

List<String> dictionary = new ArrayList<String>();

BufferedReader br = null;     

try
{
  br = new  BufferedReader(new FileReader(fileName));
  String word;

  while((word = br.readLine())!=null)
  {
      dictionary.add(word);
  }

}
catch(IOException e)
{
  e.printStackTrace();
}
String[] words = new String[dictionary.size()];
dictionary.toArray(words);

//for( int i = 0; i < words.length; i++ )
//    System.out.println(words[i]);


System.out.println("\nEnter the phrase to scramble: ");
String input = sc.nextLine();
System.out.println();


    size = input.length();
    count = 0;
    charArray = new char[size];
    for (int j = 0; j < size; j++)
      charArray[j] = input.charAt(j);
    doAnagram(size);
 }

public static void doAnagram(int newSize) 
{
 int limit;
if (newSize == 1) // if too small, return;
  return;
// for each position,
for (int i = 0; i < newSize; i++) {
  doAnagram(newSize - 1); // anagram remaining
  if (newSize == 2) // if innermost,
    printAnagrams(); 
  rotate(newSize); // rotate word
}
}


public static void rotate(int newSize) 
{
int i;
int position = size - newSize;

char temp = charArray[position];

for (i = position + 1; i < size; i++)
  charArray[i - 1] = charArray[i];

charArray[i - 1] = temp;
}

public static void printAnagrams() 
{
for (int i = 0; i < size; i++)
{    
  //System.out.print(charArray[i]);
    if(charArray[i] == words[i])
    {
        System.out.print(charArray[i]);
    }

 }
 System.out.println();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面犯桃花 2024-12-20 01:31:20
  1. 您的静态变量words未被使用,因为您在赋值之前定义了一个新的String[]words。

  2. 使用 equals 方法比较字符串。 1

  1. Your static variable words is not being used because you define a new String[] words before the assignment.

  2. Use the equals method to compare Strings. 1

来日方长 2024-12-20 01:31:20

另一个问题是,当您实际上(大概)想要测试第 i 个字谜是否存在于字典中的任何位置时,您正在将生成的第 i 个字谜与字典中的第 i 个元素进行比较。

您可以尝试对字典使用字符串的 HashSet h,而不是数组,然后使用 h.contains(...) 检查字谜的有效性。

Another issue is that you're comparing the i'th anagram generated to the i'th element in your dictionary, when you actually (presumably) want to test if the i'th anagram is present in the dictionary at any position.

You can try using a HashSet h of strings, rather than the array, for the dictionary, and then check for validity of an anagram with h.contains(...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文