Results 1 to 6 of 6
Thread: Method to "uncover"
- 05-08-2010, 01:30 AM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Method to "uncover"
Hi all,
im learning Java atm and got a problem in the following code:
uncover should uncover all characters saved in hidden and Im trying to find a method to get this done. Furthermore, case sensitivity should not matter. Im playing around with char array... but Im stuck atm :)
The previous method turns the confidential term into "_", which works fine so far.
Any help would be highly appreciated.
Java Code:public class Hangman { public static void main(String[] args) { String confidential = "Test Test Test ROAR"; String hidden = createHidden(confidential); System.out.println(hidden); hidden = uncover(confidential, hidden, 'e'); System.out.println(hidden); hidden = uncover(confidential, hidden, 'R'); System.out.println(hidden); hidden = uncover(confidential, hidden, 'a'); System.out.println(hidden); } public static String createHidden(String confidential) { String result = " "; for (int i = 0; i < confidential.length(); i++) { if (confidential.charAt(i) == ' ') { System.out.print(" "); } else { System.out.print("_ "); } } return result; } public static String uncover(String confidential, String hidden, char character) { String result = ""; String test1 = new String (hidden.toLowerCase()); //FIXME String test2 = new String (test1.toUpperCase()); //FIXME String all = new String (test1 + test2); //FIXME char [] array = all.toCharArray(); //FIXME for(int i=0; i < confidential.length(); i++) { if (confidential.equals (array)) //FIXME System.out.print(); //FIXME } return result; } }
The Output should look like:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ e _ _ _ e _ _ _ e _ _ _ _ _ _
_ e _ _ _ e _ _ _ e _ _ r _ _ r
_ e _ _ _ e _ _ _ e _ _ r _ a r
Mine so far:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
-
Get rid of the System.out.println(...) calls in your uncover and createHidden methods as they don't update the result String which is what you should be doing instead here. So in place of System.out.println, you may do better doing
Java Code:result += /* something goes here */;
- 05-08-2010, 02:18 AM #3
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Thanks for the fast reply!
Changed my code so far.
Now I want to change the if (...) statement in the second method.
My thought on this was as follows (some pseudo inc ^^)
If confidential.charAt(i) equals a character of array then return the character.
But as I cant do it like this (incomparable types: char and char [])
I dont know how to fix it.Java Code:if (confidential.charAt(i) == (array))
-
1) There is no need to call new String(...) anywhere in your code, so get rid of those constructor calls. Instead simply call a method of String, that's it.
2) What are you trying to do here as this code makes little sense to me:
3) You don't want to compare a char to array, again it makes no sense. Instead you want to compare a char with a char. Use the index of the for loop, i, inside the body of the loop to extract your char out of the array.Java Code:String test1 = new String (hidden.toLowerCase()); //FIXME String test2 = new String (test1.toUpperCase()); //FIXME String all = new String (test1 + test2); //FIXME char [] array = all.toCharArray(); //FIXME
- 05-08-2010, 03:48 AM #5
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
The code lines ending with fixme were just some "farting around". Deleted them.
All right, got rid of the Strings and hopefully i compare chars now:
Guess im taking a nap now :)Java Code:public static String uncover(String confidential, String hidden, char character) { String result = ""; char [] array1 = hidden.toCharArray(); char [] array2 = confidential.toCharArray(); for(int i=0; i < confidential.length(); i++) { if (array1[i] == array2[i]) result += (...); //FIXME } return result; }
-
don't ignore the char "character". Think on what you want to do with this variable -- perhaps you should be using it in the logic portion of your if block?
Similar Threads
-
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
[SOLVED] Why does the compiler return "not a statement" for this method body please?
By trueblue in forum New To JavaReplies: 3Last Post: 05-25-2009, 08:50 PM -
differentiate between user selection and "additem" method in combobox
By rainman3 in forum New To JavaReplies: 2Last Post: 04-28-2009, 04:52 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks