Results 1 to 10 of 10
Thread: Help with program
- 04-07-2009, 08:15 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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?
- 04-07-2009, 08:20 PM #2
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 Grüße / lepo pozdravje
8=:(=)D . · ° o O ( Save the Giraffes )
- 04-07-2009, 08:23 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
I'm sorry but I'm relatively new to java and am not sure how to sort a string like that.
- 04-07-2009, 08:28 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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)).
- 04-07-2009, 08:29 PM #5
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-07-2009, 08:55 PM #6
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.
something like that. i hope you got the pointJava 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 } }Cheers / beste Grüße / lepo pozdravje
8=:(=)D . · ° o O ( Save the Giraffes )
- 04-07-2009, 08:56 PM #7
O i totally forgot about this Arrays class. Supergood! (you see i try to work as little with arrays ;))
Cheers / beste Grüße / lepo pozdravje
8=:(=)D . · ° o O ( Save the Giraffes )
- 04-07-2009, 11:06 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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));
}
}
- 04-07-2009, 11:23 PM #9
You really don't need the following verifcation:
You could change it to something like:Java Code:if(count == first.length){ answer = true;
Luck,Java Code:for(int x = 0; x < first.length; x++){ if(first[x] == second[x]){ count++; } else { return answer; } } answer = true; return answer;
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-07-2009, 11:42 PM #10
I would also move the string length comparation to the very beginning of the code:
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.Java Code:if (w1.length() != w2.length()) { return answer; }
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks