Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    New to Java, need assistance with code

    I was wondering if anyone could help me get this code to work the way its intended.

    public class Main {
    public static void main(String[] args) {
    String a = new String("Wow");
    String b = "Wow";
    String c = a;
    String d = c;

    boolean b1 = a == b;
    boolean b2 = b.equals(b + "!");
    boolean b3 = !c.equals(a);

    if (b1 && b2 && b3) {
    System.out.println("Success!");
    }
    }
    }

    What do I need to change to make this display "Success!" when executed?

    Any help will be greatly appreciated

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Every one of those booleans are false:
    a != b (strings are immutable)
    b != b + "!" (this wouldn't ever be true)
    c = a (c and a are the same object, so checking ! is false)

    So to get it to show success you can either negate each boolean during assignment or the if check, or you can negate the entire set of conditions after the check.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Dragondd320 (12-10-2013)


  •  

    Posting Permissions

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