Search code examples
javabooleanasciicall

Not sure how to call and print ASCII art method


I want to print out an Ascii art picture from a different method. My code does not work however. Here it is:

String avatar = IBIO.inputString ("Now tell me. Are you a boy? Or are you a girl? ");


 if (avatar == "boy" || avatar == "Boy" || avatar == "b" || avatar == "B")
 { 
 System.out.print (Boy );
 }

public void Boy ()
{
 {
  System.out.println ("    /:''|    ");    
  System.out.println ("   |: 66|_   ");  
  System.out.println ("   C     _)  ");  
  System.out.println ("    \\ ._|      ");
  System.out.println ("     ) /       ");
  System.out.println ("   /`\\       ");
  System.out.println ("   || |Y|       ");      
  System.out.println ("   || |#|       ");     
  System.out.println ("   || |#|       ");  
  System.out.println ("   || |#|       ");  
  System.out.println ("   :| |=:       "); 
  System.out.println ("   ||_|,|      ");
  System.out.println ("   \\)))||     ");
  System.out.println ("|~~~`-`~~~|   ");
  System.out.println ("|         |    ");
  System.out.println ("|_________|    ");
  System.out.println ("|_________|    ");
  System.out.println ("    | ||       ");
  System.out.println ("    |_||__        ");
  System.out.println ("    (____))    ");
}
}

Also, if the user types in girl, I want to make another method and print out a girl ascii art picture.


Solution

  • First of all, you are not doing the String comparison properly. You should be using equals() or equalsIgnoreCase() and not == to compare the Strings.

    Secondly, You should call Boy method as follows: Boy();. No need of enclosing it into System.out.print();.

    Here is the corrected code snippet:

    if ("boy".equalsIgnoreCase(avatar) || "b".equalsIgnoreCase(avatar)) {
        Boy();
    }
    

    Note that I'm using equalsIgnoreCase() here instead of just equals() and hence you need not to do the comparisons for String such as Boy, BOY or B etc.