So I have to write a two class program that reads in two words or phrases from the keyboard, and then judges if the first phrase is an anagram of some of the letters in the second phrase.
Examples:
*mo / moo (yes)
* moo / mo (no)
* rip / zipper (yes)
* Clint Eastwood / Old west Action! (yes)
Algorithm Idea #1: make a scoreboard for the letters a to z. Every time you encounter a letter in the second String, up its count by 1; Then, every time you encounter a letter in the first String, lower its count by 1. Accept if the scoreboard ends up with all entries >= 0.
So far i have this but the driver class wont compile... Any help would be appreciated.
import java.util.*;
import java.util.*; public class SubAnTester{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Enter first String"); String str1 = scan.nextLine(); System.out.println("Enter second String"); String str2 = scan.nextLine(); strObject compstr = new strObject(str1, str2); str1.toLowerCase(); str2.toLowerCase(); compstr.runTesters(); compstr.reportResult(); } }
and the second class is
2 errors found:public class SubAnagram{ private String str1; private String str2; private int pos; private char ch; private boolean posTester; public String strObject(String shorter, String longer){ str1 = shorter; str2 = longer; } int[] charTally = new int[26]; public void array1Tester(){ for(int j=0; j<str1.length();j++){ ch = str1.charAt(j); if(Character.isLetter(ch)){ pos = ch - 'a'; charTally[pos]++; } } } public void array2Tester(){ for(int j=0; j<str2.length();j++){ ch = str2.charAt(j); if(Character.isLetter(ch)){ pos = ch - 'a'; charTally[pos]--; } } } public void runTesters(){ array2Tester(); array1Tester(); posTester(); } private boolean posTester(){ boolean isAnagram=true; for(int j=0; j<charTally.length; j++){ while(charTally[j]>=0){ return true;} if(charTally[j]<0){ return false; } } return isAnagram; } public void reportResult(){ if(posTester){ System.out.println(str1+" / "+str2+" (yes)"); } else{ System.out.println(str1+" / "+str2+" (no)"); } } }
File: C:\Users\Korobkov\SubAnTester.java [line: 12]
Error: C:\Users\Korobkov\SubAnTester.java:12: cannot find symbol
symbol : class strObject
location: class SubAnTester
File: C:\Users\Korobkov\SubAnTester.java [line: 12]
Error: C:\Users\Korobkov\SubAnTester.java:12: cannot find symbol
symbol : class strObject
location: class SubAnTester