Results 1 to 20 of 21
- 09-28-2011, 10:33 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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?
- 09-28-2011, 10:41 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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));
- 09-28-2011, 10:51 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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 ? :)
- 09-28-2011, 11:05 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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.
- 09-28-2011, 11:45 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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!
- 09-28-2011, 11:57 PM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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.
- 09-29-2011, 12:54 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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!
- 09-29-2011, 01:02 AM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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...
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.
- 09-29-2011, 01:43 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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
- 09-29-2011, 02:05 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Re: Help with beginner Java program? Boolean?
yes, but I'm having trouble writing the actual method....
-
Re: Help with beginner Java program? Boolean?
- 09-29-2011, 02:29 PM #12
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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 05:54 PM. Reason: Added code tags, [code]...[/code]
- 09-29-2011, 08:57 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
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!
-
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();
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.
- 09-29-2011, 11:01 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
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...
-
Re: Help with beginner Java program? Boolean?
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?
- 09-29-2011, 11:32 PM #17
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
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?
- 09-29-2011, 11:52 PM #18
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 11
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.
- 09-30-2011, 02:37 AM #19
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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?)....
- 09-30-2011, 06:51 PM #20
Member
- Join Date
- Sep 2011
- Posts
- 16
- Rep Power
- 0
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 gettingDog and *** -> trueLast edited by ayelleeeecks; 09-30-2011 at 09:41 PM.
Similar Threads
-
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 01:01 AM -
JAVA Beginner - Simple Program help
By Logik22 in forum New To JavaReplies: 13Last Post: 07-15-2011, 03:44 PM -
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 10:04 AM -
Java boolean problem
By javagangster in forum New To JavaReplies: 1Last Post: 04-04-2011, 01:24 PM -
Beginner Needs Help w/ Program for School
By badness in forum New To JavaReplies: 2Last Post: 11-24-2007, 08:51 PM
Bookmarks