Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: boolean statement in do loop not workling for string

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default boolean statement in do loop not workling for string

    Please tell this beginning coder why this do loop runs infinitely. It's supposed to break out into the switch statement when the user enters a "y" but it keeps looping no matter what. Thanks

    public static boolean YesNo (String query) {

    Scanner localScanner = new Scanner(System.in); // Create a Scanner object

    boolean temp = false;
    String c = "Y";
    do {
    System.out.print(query);
    c = localScanner.next();
    c = c.toUpperCase();
    } while (c != "Y"); //THIS DOESN'T WORK


    switch (c) {
    case "Y":
    temp = true;
    case "N":
    temp = false;
    }

    return temp;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    On vacation
    Posts
    24,712
    Thanks
    57
    Thanked 2,678 Times in 2,628 Posts

    Default Re: boolean statement in do loop not workling for string

    Use the equals method to compare the contents of two String objects.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: boolean statement in do loop not workling for string

    Just my recommendation. Dont need to convert to uppercase.
    The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences

Similar Threads

  1. What to return after in this boolean method with the if/else statement?
    By jenniferruurs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2020, 09:28 AM
  2. Replies: 2
    Last Post: December 10th, 2019, 06:17 AM
  3. Random Boolean in an if statement (tricky, can't seem to figure out)
    By Xhalite in forum Loops & Control Statements
    Replies: 6
    Last Post: May 29th, 2013, 07:28 AM
  4. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  5. Help with Loop Structures, If-else statement, and String
    By javabeg123 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2011, 10:01 PM