Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree2Likes

Thread: Help with beginner Java program? Boolean?

  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Help with beginner Java program? Boolean?

    For this assignment you are to write a single class, with main included. The job of the class is as follows: you are to read in two strings from the keyboard, string1 and string2, and then you are to determine if string1 and string2 are equal, character for character. Your program should report its answer as true or false. Note that if the strings are different lengths, then they are definitely not equal.

    There are two "catches" to the problem. First, capitalization in your solution should not matter. Thus if the strings are "dog" and "DoG", then your program should report true - they are equal. The second catch concerns a wild card symbol, here the character '*'. If * appears in one of the strings, then it can match any other character. So "Dog" and D*g" should be judged as equal, since the *, as a wild card, matches any other character, in this case the 'o'.

    More examples:

    Dog and d** -> true
    Dog and *** -> true
    Dog and **** -> false (different lengths)
    bla*k and B***k -> true


    One further suggestion: use theScanner method nextLine() rather than the method next() for reading input.

    Your solution should be in a single class containing main, and that class MUST BE called WildEquality. Enter (paste) your code in the box below. Note that the import statement for Scanner is provided. DO NOT include any import statements in the code you submit.


    Sorry I know that's long to read!
    I figured out the scanner part, but I'm having trouble with the boolean part.

    if (string1==string2){
    System.out.println("true");
    }else{
    System.out.println("false");
    }

    I know this is wrong right now- I don't get how to get it to sees the strings in terms of characters...any help?

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Help with beginner Java program? Boolean?

    Don't use == to compare strings. It compares object references instead of string content. Use .equals() instead.

    Also, there's no need to use an if-else statement there; instead of the fragment above, you could simply use:
    Java Code:
    System.out.println(string1.equals(string2));
    sunde887 likes this.

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    thats works!
    I don't get how to get it to say true through for Dog and dog or D*g and dog....any hints ? :)

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Help with beginner Java program? Boolean?

    For "Dog" and "dog" to be considered equal, the equalsIgnoreCase() method will do the job. You'll need to write your own method to take wildcard characters into consideration though.

    Hint: look at each character of the string separately.

  5. #5
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    ok so to make the method...
    I'm not seeing it.
    Do I set *= to all letters or something? I can't figure it out...

    But thanks for all your help!

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Help with beginner Java program? Boolean?

    The two strings are unequal on one of two conditions:
    1: The lengths of the strings are different.
    2: For any character in one string, neither of the following statements are true:
    -- The character is equal to its counterpart in the other string, ignoring case
    -- Either the character or its counterpart is a wildcard

    With that in mind, try writing a method with the header boolean equalsIgnoreCaseWithWildcard(String str1, String str2, char wildcard). An enhanced-for loop might come in handy, as well as the toCharArray() and toLowerCase() methods of String.

  7. #7
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    the toCharArray turns the string to characters?
    and I only know basic loop like for(int j=0;j<3, j++)....
    It's just figuring out how the wildcard fits into all this...
    But Thanks so much again!

  8. #8
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Help with beginner Java program? Boolean?

    the toCharArray turns the string to characters?
    Yep. You can't iterate over the characters of a String, but you can iterate over a char[] array.

    and I only know basic loop like for(int j=0;j<3, j++)....
    That's fine, you can do it that way if you prefer, but it's useful to know that the other way exists. Read about it here.

    It's just figuring out how the wildcard fits into all this...
    Again, you'll have to look at each character separately by looping over them and see whether each character is a wildcard.

    Edit: On second thoughts, you can't use enhanced-for to do what you're trying to do. Just use the way you already know.

  9. #9
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    ok really thank you for all your help. It's my 3rd week of Java, so its a lot to take in! (and this is only my 3rd program)...

    It's amazing how confused I am about this wildcard part...would it be something like for(CharAt=0, char<string1,char=*)...I know the syntax is wrong...and ok at this point I think I've driven you crazy enough, time to visit the TA! But once again thanks, you've gone above and beyond. I can't wait until I get to the point where I look back at this like- wow this program was so easy but I thought it was impossible back then

  10. #10
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    yes, but I'm having trouble writing the actual method....

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with beginner Java program? Boolean?

    Quote Originally Posted by katiebear128 View Post
    yes, but I'm having trouble writing the actual method....
    Without seeing your attempt, it's impossible to know how to help you.

  12. #12
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    ok, here is what I have so far:
    Java Code:
    import java.util.Scanner;
    
    public class WildEquality
    {public static void main (String [] args)
      {
      Scanner console=new Scanner(System.in);
      System.out.println("Enter a string");
      String string1;
      String string2;
      string1=console.nextLine();
      string2=console.nextLine();
      
      char star ='*';
      int j;
      int length=string1.length();
      
      string1.toCharArray();
      string2.toCharArray();
      
      
      for (j=0;j<string1.length();j++)
      {
        if(string1.charAt(j) !='*')
          
          
          System.out.println(string1.equalsIgnoreCase(string2));
        
      } 
    }
    }

    Now I need to make a WildCard method.
    so for the header...
    public boolean ignoreWildcard(String string1, String string2, char wildcard)
    {if (isWildcard( ....I'm lost here...

    would this be the start of a header? Then later I would have to define isWildcard method...
    Last edited by sunde887; 09-29-2011 at 04:54 PM. Reason: Added code tags, [code]...[/code]

  13. #13
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    This is also my third programming assignment, we must be enrolled in the same course. I can't figure out the wildcard method either. I've been trying to use two different for loops to see if there is any '*'s in string1 and string2. I'm trying to get the program to recognize when there is a wildcard in either string, then when it does recognize it, I'm trying to use charAt() to assign the wildcard the character at the same "charAt" in the other string.

    I'm so lost. It won't work. I'm going to stay tuned to this forum to see if there are any further responses!

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with beginner Java program? Boolean?

    A couple of issues:

    You understand that these two lines of code do nothing but waste space:

    Java Code:
    string1.toCharArray();
    string2.toCharArray();
    Strings are immutable (unchangable), and while these method calls return a char array, you don't do anything with them, and of course do not change the underlying Strings.

    Next, your for loop:

    Java Code:
      for (j=0;j<string1.length();j++)
      {
        if(string1.charAt(j) !='*')
           
           
          System.out.println(string1.equalsIgnoreCase(string2));
         
      }

    You can and should do a lot more in this loop. Think about the logic you'd use if you were doing this on paper with a pen checking two very long strings for a similar equality. What steps would you take? Write these steps on paper, and then translate them to Java, and you'll have the start of your algorithm.

  15. #15
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    So the for loop we're creating should be able to do what? My code already checks to see if the strings are the same length, and if they're not, then the two strings are definitely not equal, so the rest doesn't matter, right? But the for loop should check each character in each string to see if there are any wildcard "*"s, and what if there is? I'm still unclear as to how the program is supposed to be able to change the "*" to any other character to match the other string...

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with beginner Java program? Boolean?

    Quote Originally Posted by ayelleeeecks View Post
    So the for loop we're creating should be able to do what? My code already checks to see if the strings are the same length, and if they're not, then the two strings are definitely not equal, so the rest doesn't matter, right? But the for loop should check each character in each string to see if there are any wildcard "*"s, and what if there is? I'm still unclear as to how the program is supposed to be able to change the "*" to any other character to match the other string...
    Again, put away Java for a moment and think what you have to do on paper in this similar situation. Say you've got two Strings on paper that are over 300 characters long without spaces, and your job is to go through both strings, character by character (in Java, this would be by using a loop), and checking if the characters in each String are the same (irrespective of capitalization) or if one is a '*'. So again, what are the logic steps involved, not the coding steps?

  17. #17
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    I'd have to go through character by character comparing each of the strings. So I'd look at the first character in the string1 and see if its the same as the first character in string2. If it is the same, I continue to do the same process until I get to a point where the characters are not the same. If, at this point, the characters are not the same AND neither of the characters is a '*', then the two strings are not equal, and I can stop the loop there. If that characters are not the same but the character in one of the strings IS a "*", then we'd have to tell the program to set the "*" equal to whatever the character in the other string is set as, and then continue with this process until the end of the strings. Is this the correct logic? Or is there something simpler?

  18. #18
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Help with beginner Java program? Boolean?

    If a character at a given position of either string is a wildcard, you can treat it exactly the same as if they were matching characters. You don't need to modify any characters.
    Fubarable likes this.

  19. #19
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    so I need to make a method in the loop, something like..if *, then, continue the loop." tell the program to set the "*" equal to whatever the character in the other string is set as, and then continue with this process until the end of the strings." I was thinking along this line too...but Iron Lion said to treat it the same as if they were matching characters...so we need the program to recognize that if it sees a star, to just continue the loop..Right now my code in my loop, says to stop if it sees a *(right?)....

  20. #20
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Help with beginner Java program? Boolean?

    Right, you basically want the loop to ignore any *s. The program that every situation is true UNLESS the words are different lengths AND/OR the lettered characters at position x in both strings are not equal.

    The problem I'm having right now is getting
    Dog and *** -> true
    to work. The other examples are working fine, so I'm not sure why the loop isn't working when the word starts with a wildcard :(
    Nevermind! Got it to work, there was just a minor issue with my logic, thanks for all the help! :)
    Last edited by ayelleeeecks; 09-30-2011 at 08:41 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  2. JAVA Beginner - Simple Program help
    By Logik22 in forum New To Java
    Replies: 13
    Last Post: 07-15-2011, 02:44 PM
  3. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  4. Java boolean problem
    By javagangster in forum New To Java
    Replies: 1
    Last Post: 04-04-2011, 12:24 PM
  5. Beginner Needs Help w/ Program for School
    By badness in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:51 PM

Tags for this Thread

Posting Permissions

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