Results 1 to 10 of 10
  1. #1
    xnickx5x is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default Help with program

    I have to construct a program that reads in two strings and determines if they are Anagrams (for example cat / act) should be true. I have made a constructer class named Anagram and a driver class AnagramDriver. I want to convert two strings that are entered on the keyboard into arrays and then compare the first one with the second to see if they contain the same letters. However I am having trouble doing this. Here is my code so far...

    import java.lang.String;

    public class Anagram{
    private String word1;
    private String word2;

    public Anagram(String first, String second){
    word1 = first;
    word2 = second;
    }

    public boolean compareWords(String word1, String word2){
    boolean answer = true;
    int count = 0;
    word1.toLowerCase();
    word2.toLowerCase();
    char[] uno = word1.toCharArray();
    char[] dos = word2.toCharArray();



    I am not sure of what to do next, does anyone have any suggestions?

  2. #2
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default Sorten verschmorten

    Hi

    did you think about sorting your Strings internally (perhaps thats the reason for the arrays). because after sorting
    "cba" and "bac" would be the same (abc, abc) which you can test by equals(). When sorting I would ignore the case so Abc equals abc. Also strings of different lenght would not qualify to be an anagram.

    I hope it helps :)
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

  3. #3
    xnickx5x is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    I'm sorry but I'm relatively new to java and am not sure how to sort a string like that.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    well, i was gonna just hint at sorting, but since you already said it...

    you have a char array. chars are comparable (==, <, >, etc.). just use any sorting algorithm on your array (or the one provided in Arrays (Java 2 Platform SE v1.4.2)).

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Building on mcfrog's suggestions, you can use the sort() method from the Arrays class:

    • Arrays class & methods:

    Arrays (Java 2 Platform SE v1.4.2)
    • Examples:

    Java: Sorting Arrays

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default The thinking part is yours :)

    well, you already created two char arrays but I would go for the collection framework because insertion and deletion is much easier than for arrays which need new creation.
    Java Code:
    private String sortMyArray(final char[] theChar) {
    //"foreach" loop since java 5
      ArrayList<String> list = new ArrayList<String>();
      for(Character c : theChar) { //autoboxing from char to Character
        list.add(c.toString());
      }
      String result = "";
      String tempS = "";
      for(String s : list) {
    //fill tempS and compare with s
    //compare: see Java api of String
    //if equal or smaller put it into result 
      }
    }
    something like that. i hope you got the point
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

  7. #7
    mcfrog's Avatar
    mcfrog is offline Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    32
    Rep Power
    0

    Default

    O i totally forgot about this Arrays class. Supergood! (you see i try to work as little with arrays ;))
    Cheers / beste Gre / lepo pozdravje
    8=:(=)D . o O ( Save the Giraffes )

  8. #8
    xnickx5x is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    I've completed the program thanks to all of your help but I still have one little problem. I want to be able to disregard any type of punctuation that is entered in either of the two strings. This is my code for both the Anagram class and the AnagramDriver class...

    import java.util.*;
    import java.lang.String;

    public class Anagram{

    public Anagram(){
    }

    public boolean compareWords(String w1, String w2){
    boolean answer = false;
    int count = 0;
    String uno = w1.toLowerCase();
    String dos = w2.toLowerCase();
    String uno2 = uno.replace(" ", "");
    String dos2 = dos.replace(" ", "");
    char[] first = uno2.toCharArray();
    char[] second = dos2.toCharArray();
    Arrays.sort(first);
    Arrays.sort(second);
    if(first.length != second.length){
    return answer;
    }
    else{
    for(int x = 0; x < first.length; x++){
    if(first[x] == second[x]){
    count++;
    }
    }
    if(count == first.length){
    answer = true;
    }
    return answer;
    }
    }
    }


    And the AnagramDriver class

    import java.util.*;

    public class AnagramDriver{

    public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter first phrase or word");
    String entry1 = scan.nextLine();
    System.out.println("Enter second phrase or word");
    String entry2 = scan.nextLine();
    Anagram myAnagram = new Anagram();
    System.out.println("Anagram? --" + " " + myAnagram.compareWords(entry1, entry2));
    }
    }

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    You really don't need the following verifcation:
    Java Code:
    if(count == first.length){
    answer = true;
    You could change it to something like:
    Java Code:
    for(int x = 0; x < first.length; x++){
    if(first[x] == second[x]){
    count++;
    }
    else {
            return answer;
           }
    }
    answer = true;
    return answer;
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I would also move the string length comparation to the very beginning of the code:
    Java Code:
    if (w1.length() != w2.length())
      {
       return answer;
      }
    That way if there is a difference in the length, you don't spend any CPU processing the lowerCase, replace, array creation, arrays sort, etc.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •