Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: anagram program wont compile please help

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default anagram program wont compile please help

    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
    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)");
          }
        }
      }
    2 errors found:
    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


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: anagram program wont compile please help

    I'm going to try and help by asking questions.
    First tackle the error message... What does it tell you?
    Line 12, can't find a symbol.
    Line 12 is:

    strObject compstr = new strObject(str1, str2);

    The error says it can't find a symbol named strObject, do you have a class named strObject? If so, where?

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: anagram program wont compile please help

    so the strObject was meant to be a constructor so i renamed it to the name of the class it resides in and it looks like this:

    public class SubAnagram{

    private String str1;
    private String str2;
    private int pos;
    private char ch;
    private boolean posTester;

    public String SubAnagram(String shorter, String longer){
    str1=shorter;
    str2=longer;
    }
    etc....

    Except this time the error message is asking for a return type even though its a constructor...
    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();
    SubAnagram compstr = new SubAnagram(str1, str2); // line indicated in second error
    str1.toLowerCase();
    str2.toLowerCase();
    compstr.runTesters();
    compstr.reportResult();

    }
    }
    2 errors found:
    File: C:\Users\Korobkov\SubAnagram.java [line: 12]
    Error: C:\Users\Korobkov\SubAnagram.java:13: missing return statement
    File: C:\Users\Korobkov\SubAnTester.java [line: 12]
    Error: C:\Users\Korobkov\SubAnTester.java:12: cannot find symbol
    symbol : constructor SubAnagram(java.lang.String,java.lang.String)
    location: class SubAnagram
    Last edited by Rusak; March 25th, 2011 at 11:42 PM.

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: anagram program wont compile please help

    You may want to relook at how you're making that constructor, take a look at my example and compare the form of my constructor to yours:
    //Filename: Class1.java
    public class Class1{
    	public Class1(){ //this is a constructor
    		System.out.println(method1()); //a call to method1 inside a println
    	}
    	public String method1(){ //method method1, this line says it will return a string
    		return "Hello World!"; //return a string
    	}
    }

    Things to note, the name of the constructor should be the same as the name of the class and filename. Java naming nomenclature also suggests capitalizing the first letter of a class. But in the case where SubAnTester was not created by you, and you can't edit the tester file, you can make it lowercase. Method names generally start lowercase.

    hope this helps.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: anagram program wont compile please help

    Thanks guys but by the time you replied i figured out everything except for this...
    My code compiles fine but it always returns yes. the method posTester and reportResult need to be looked at if you could please help. booleans are so confusing sometimes when it comes to print statements. heres the new code. driver class
    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();
        SubAnagram c = new SubAnagram(str1, str2);
        str1=str1.toLowerCase();
        str2=str2.toLowerCase();
        c.runTesters();
        c.reportResult();   
      }
    }
    and the second class
    public class SubAnagram{
     
      private String str1;
      private String str2;
      private int pos;
      private char ch;
      private boolean posTester;
     
      public SubAnagram(String subString, String largeString){
        str1=subString;
        str2=largeString;
      }
      private int[] charTally = new int[26];
     
      public  void array1Tester(){
        str1=str1.toLowerCase();
        for(int j=0; j<str1.length();j++){
          ch = str1.charAt(j);
          if(Character.isLetter(ch)){
            pos = ch - 'a';
            charTally[pos]++;
          }
        }
      }
      public  void array2Tester(){
        str2=str2.toLowerCase();
        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<26; j++){
          if((charTally[pos]<0)){
            isAnagram=false;
            break;
          }
        } return isAnagram;
      }
     
      public void reportResult(){
        if(posTester()){
          System.out.println(str1+" / "+str2+" (yes)");
        }
        else{
          System.out.println(str1+" / "+str2+" (no)");
        }
      }
    }
    any help would be appreciated.

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: anagram program wont compile please help

    The current issue is in posTester(), this line :
     if((charTally[pos]<0)){

Similar Threads

  1. Anagram program wont compile
    By Rusak in forum Member Introductions
    Replies: 0
    Last Post: March 25th, 2011, 03:38 PM
  2. program wont renew total
    By that_guy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 01:00 PM
  3. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM
  4. *why won't this compile?*
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 2nd, 2010, 08:18 PM
  5. need help to compile
    By hardwarewizard in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 10:03 AM

Tags for this Thread