Saturday 15 June 2013

java - A couple questions regarding anagrams -


I have followed some of the questions I asked earlier. Before that I asked how to use a method to determine whether the elements of a list are present in the other or not. Because of this I have done this because I want to define that a list has drawings of others - using user input and a dictionary list. I am having difficulty in determining this, I want to pass the lists in a list to determine whether one contains the elements of another, or not. Right now it only gives false false when I know that the actual anagram is present.

Can anyone see my code and help me? Besides, how do I determine that the user input has many-word anagrams?

My algorithm works by taking two lists and by describing the letters in each word, I read about this method in another post and decided to go with it - this work is in progress is. This is my code:

  Public class AnagramSolver1 {Public static zero main (string [] args) IOException throws {// scanner / reader scanner scan = new scanner (System.in); BufferedReader = In the new BufferedReader (New FileReader ("src / dictionary.txt")); // lists containing unordered dictionaries and inputs, and after the alphabetical order list & lt; String & gt; Dictionary = New Arrestist & lt; String & gt; (); & Lt; Four []> Dictionary: Source = New Arrestist & lt; Char [] & gt; (); & Lt; String & gt; Input List = New Arrestist & lt; String & gt; (); & Lt; Four []> InputSort = New Arrestist & lt; Char [] & gt; (); String line = null; // Read in the dictionary and then in the alphabetical order //// while (zero! = (Line = in.readline ())) {dictionary.add (line); } In.close (); Dictionary sort = sortlist (dictionary); // print statement / * for (int i = 0; i & lt; dictionary structure; i ++) {System.out.println (dictionary:} * / // user input, then sort alphabetically // // System.out.println ("Enter the word:"); string input = scan.next (); inputList.add (input); InputSort = sortlist (input list); // for print statement * = 0; I & lt; Input Asiose (); I ++} {System.out.println (inputSort.get (i));} * / // Define that user input is the fingerprint of any dictionary word boolean =Anagram (dictionaryResearch, InputSort); Println (isAnagram);} // Sort a string in four letters; Public static list & lt; char [] & gt; sortlist (list & lt; string & gt; sort) { List & lt; char [] SortList = new arreelist & lt; char [] & gt; (); for four [] letterSort; (int i = 0; i & lt; sort.size (); i ++) {letterSort = sort.get (i) .to Character (); Arrays.sort (letterSort); sortList.add (letterSort);} Return SorList;} // Determine if the user input is an anagram or not Public stable boolean isagraag (list & lt; four []> dictionaryartort, list & lt; four []> inputSort) {for (four [] c: dictionarySort) {if (inputSort.contains (c)) { Return true; } } return false; }}    

Here's a big problem:

  (Four [] c: dictionary sort) {if (inputSort.contains (c)) // // bug!   

It will not work as you think it will be. This test is included in the exact four [] item list; It does not test if a letter contains the same character.

The way you intend to do it, char [] , store strings to do it.

To convert a char [] to a string , do this:

  four [] ; String s = new string (s);   

Once you solve your characters in an array, change the four arrays into a string and place it in your lists. This means that in all lists list & lt; String & gt; should be in the list.

No comments:

Post a Comment