Thursday, February 9, 2012

JAVA STRING TEXT TWIST?

how will you know if all the letters in the input2 (String) matches with the input1 (String also), ignoring case, with respect to the count of the input1's letters as well.





For example:

VALID:

Input1 = KOREA



input2 = Ore

or

input2 =oRe

or

input2 = KoR

or

input2 = ArE





INVALID:

input1 = KOREA



input2 = WAT

or

input2 = REEEEE

or

input2 =koreaA

input2 =KoRReAAA





how will i show it?





I'm making a textTwist in java. anyone HELP!

:((((



no letters also should duplicate even if the characters of input2 is in input1JAVA STRING TEXT TWIST?
If you want to ensure that the letters in input2 are in exactly the same order as those in input1, then the following below should work:



if (input1.toLowerCase().contains(input2.to鈥?{

// this is valid - do something

}



However, if you want to ensure that input2 contains one or more letters that are present in input1, but no letters that aren't present in input1, and that each unique letter in input2 occurs no more than once, and you aren't fussed about the order of the letters in input2 compared with the order of the letters in input1, then the following should work:



public boolean isValid(String input1, String input2) {

input1 = input1.toLowerCase();

input2 = input2.toLowerCase();

boolean valid = true;



Set%26lt;String%26gt; input1Letters = new HashSet%26lt;String%26gt;();

for (int i = 0; i %26lt; input1.length(); i++) {

char[] theChar = new char[] {input1.charAt(i)};

input1Letters.add(new String(theChar));

}



Set%26lt;String%26gt; input2Letters = new HashSet%26lt;String%26gt;();

for (int i = 0; valid %26amp;%26amp; i %26lt; input2.length(); i++) {

char[] theChar = new char[] {input2.charAt(i)};

String currentLetter = new String(theChar);

if (!input1Letters.contains(currentLetter)) {

valid = false;

} else if (!input2Letters.contains(currentLetter)) {

input2Letters.add(currentLetter);

} else {

valid = false;

}

}



return valid;

}JAVA STRING TEXT TWIST?
Since the order of the letters of input2 doesn't matter, you'd have to iterate through the characters of input2 and see if they all exist in input1. Also, you'd need to keep a set of letters you checked of input2 to keep track of duplicates. Note that I recommended a set because the definition of a set is that all its values are unique.JAVA STRING TEXT TWIST?
Here's the method you'd need to use.



boolean equalsIgnoreCase(String anotherString)

No comments:

Post a Comment