Results 1 to 6 of 6
  1. #1
    Quelsor is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default 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:

    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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 */;

  3. #3
    Quelsor is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    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 [])

    Java Code:
    if (confidential.charAt(i) == (array))
    I dont know how to fix it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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:
    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
    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.

  5. #5
    Quelsor is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    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:

    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;
            }
    Guess im taking a nap now :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  2. Replies: 3
    Last Post: 05-25-2009, 09:50 PM
  3. Replies: 2
    Last Post: 04-28-2009, 05:52 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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