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 12 of 12

Thread: Looping through boolean Arrays

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Looping through boolean Arrays

    Hey guys, I've been working on this project for a couple days now and I can't get it right. It's due in 5 hours :\

    Okay, so the assignment is to code a "weak" anagram tester - the program should take two Strings in and print YES if they are weak anagrams of each other, and NO if otherwise.

    My teacher is defining a "weak anagram" as any phrases which use the same letters. Punctuation and spaces ignored, this program should check if the two words share the same characters.

    For example,

    ABC!$ | abc = YES
    aaabbbccc | abc = YES
    abcd | abc = NO

    etc... You get the point.

    My professor suggested that we create two boolean arrays, which I have done. If each element of the arrays corresponds to a different char, I want to make the elements true which correspond to characters in the entered phrases. However, I am having trouble doing so.

    Here is my code:

    import java.util.*;
     
    public class WeakAn2Tester{
     
      public static void main (String[] args){
     
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter two words or phrases");
    String firstLine = scan.nextLine();
    String secondLine = scan.nextLine();
    WeakAnTester2 ana = new WeakAnTester2(firstLine, secondLine);
    ana.False();
    ana.Sort();
    ana.isAnagram();
    }
    }

    and

    public class WeakAnTester2{
     
    int f; int g;
    boolean[] firstArray = new boolean[26];
    boolean[] secondArray = new boolean[26];
    String one; String two;
     
    public WeakAnTester2(String line1, String line2){
      one = line1;
      two = line2;
    }
     
    public void False(){ // Sets each element of the two arrays to false
      for (int j=0; j<26; j++){
      firstArray[j] = false;
      secondArray[j] = false;
    }
    }
     
    public void Sort(){ // Entries to lowercase, remove punctuation.
      one = one.toLowerCase().replaceAll("\\W", "");
      two = two.toLowerCase().replaceAll("\\W", "");
      //  Sets true the values of firstArray & second Array which correspond to characters of entries
      // These loops are not doing anything at the moment, but I don't know why
      for(int k=0; k<one.length(); k++){
      for(int j=0; j<26; j++){
        if (one.charAt(k) == (char)('a'+j));
        firstArray[j] = true;
      }
    }
      for(int k=0; k<two.length(); k++){
      for (int j=0; j<26; j++){
        if (two.charAt(k) == (char)('a'+j));
        secondArray[j] = true;
      }
    }
    }
     
    public void isAnagram(){ 
      for (boolean b : firstArray){
        if (b == true){
          f++;
        }
      }
      for (boolean b : secondArray){
        if (b == true){
        g++;
      }
      }
      if (g == f)
      System.out.println("YES");
      else System.out.println("NO");
    }
    }

    Right now, the code always returns "YES" because the ints g and f are always 0.
    I really appreciate anyone who takes the time to read all of this and help me out!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping through boolean Arrays

    Can you describe the algorithm (list of steps) to that the program needs to take to solve the problem?

    The code has warnings when compiled that need to be fixed. Use the -Xlint compiler option to see the warnings. Here is the command line I use to compile:
    javac.exe -cp . -Xlint WeakAnTester2.java
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Looping through boolean Arrays

    Quote Originally Posted by Norm View Post
    Can you describe the algorithm (list of steps) to that the program needs to take to solve the problem?
    Right, so I created two 26 element boolean Arrays - firstArray and secondArray. I want each element of the array to correspond to a character, the 0th element to 'a', 1st to 'b', etc. In my for loops, I am trying to set the elements of each array to True if the characters in the corresponding String are present. For example, if String one = abcd, firstArray should hold true for the 4 corresponding elements. Then I want to compare firstArray and secondArray and if they are equal, the two entries are weak anagrams of one another.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping through boolean Arrays

    What are the contents of the two arrays after the Sort() method assigns values to them?
    The Arrays class's toString() method is useful for formatting arrays for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Looping through boolean Arrays

    Quote Originally Posted by Norm View Post
    What are the contents of the two arrays after the Sort() method assigns values to them?
    Well, they are all False, because the loops apparently aren't working properly. However, I want to initiate them to False before initiating the for loops and setting the elements to True which correspond to the characters in the two entries.

    Quote Originally Posted by Norm View Post
    The Arrays class's toString() method is useful for formatting arrays for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    How could I implement this?

    Off topic: I just remembered I have to go to a concert for an hour or so, and I will be back working frivolously until midnight to get this working! If you or someone is still online then if I haven't figured it out I'll really appreciate it!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping through boolean Arrays

    Did you see this from post#2?
    The code has warnings when compiled that need to be fixed. Use the -Xlint compiler option to see the warnings. Here is the command line I use to compile:
    javac.exe -cp . -Xlint WeakAnTester2.java
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Looping through boolean Arrays

    Quote Originally Posted by Norm View Post
    Did you see this from post#2?
    The code has warnings when compiled that need to be fixed. Use the -Xlint compiler option to see the warnings. Here is the command line I use to compile:
    javac.exe -cp . -Xlint WeakAnTester2.java
    Sorry, I missed that. I don't have any compile errors, and I'm not sure what you mean by warnings. What is an -Xlint compiler option? I'm using DrJava and can't find that option.. I went to help -> DrJava errors and it didn't find any.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping through boolean Arrays

    The javac program has lots of options that control how and what it does. See the API doc:
    http://docs.oracle.com/javase/7/docs...ows/javac.html

    I don't know anything about DrJava. I use the javac program to compile with the commadline I post earlier.
    The -Xlint option shows some important warnings about your code. You need to fix DrJava to give you those warnings.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Looping through boolean Arrays

    Quote Originally Posted by Norm View Post
    The javac program has lots of options that control how and what it does. See the API doc:
    javac - Java programming language compiler

    I don't know anything about DrJava. I use the javac program to compile with the commadline I post earlier.
    The -Xlint option shows some important warnings about your code. You need to fix DrJava to give you those warnings.
    Okay, I figured out how to do that. This is the message that I received:

    WeakAnTester2.java:27: warning: [empty] empty statement after if
    if (one.charAt(k) == (char)('a'+j));
    ^
    WeakAnTester2.java:33: warning: [empty] empty statement after if
    if (two.charAt(k) == (char)('a'+j));
    ^
    WeakAnTester2.java:1: error: error while writing WeakAnTester2: WeakAnTester2.cl
    ass (Access is denied)
    public class WeakAnTester2{
    ^
    1 error
    2 warnings

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping through boolean Arrays

    warning: [empty] empty statement after if
    The ; ends the if statement and because there is not statement between the ) and the ; its empty. Remove it.

    error while writing WeakAnTester2: WeakAnTester2.class (Access is denied)
    Was another program using the class file so the compiler could not write to it?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    jwr (November 12th, 2013)

  12. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Looping through boolean Arrays

    Quote Originally Posted by Norm View Post
    The ; ends the if statement and because there is not statement between the ) and the ; its empty. Remove it.



    Was another program using the class file so the compiler could not write to it?
    Norm, I love you... I don't know how I didn't notice that about the ; after the if statement. This fixed my whole code, I had it right all along!
    (And yes I closed the program and that error went away)

    Thank you so much

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Looping through boolean Arrays

    You're welcome.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: October 20th, 2013, 02:58 AM
  2. DO WHILE LOOPING
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 22nd, 2013, 09:57 AM
  3. Arrays/Looping probelm?
    By dx8292 in forum Collections and Generics
    Replies: 7
    Last Post: February 27th, 2012, 08:44 AM
  4. looping and arrays
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 23
    Last Post: January 7th, 2011, 10:16 PM
  5. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM