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 2 of 2

Thread: Nested if esle_Statement not working as desired.

  1. #1
    Junior Member
    Join Date
    Sep 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Nested if esle_Statement not working as desired.

    Hi Professional, Hope you're doing well today.

    In the program, no errors.
    PASS, CLEARED user inputs are working as desired.
    But when I enter NO as user input in the nested if, it picks "else statement" correctly, but also control is going inside, and executing the next S.O.P display like "Have you CLEARED second level of interview?" which is not required when the first level interview is not cleared.
    Below is the code, could you please check and help me out if possible.

    package controlStatements;

    import java.util.Scanner;

    class nested_if_else_Statement {

    static public void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    System.out.println("Online Assesment, enter PASS or FAIL?");
    String oa = scan.nextLine();
    if (oa.equals("PASS")) {
    System.out.println("Please wait in the office lobby for further levels of interview.");

    System.out.println("Have you CLEARED first level of interview?");
    String level1 = scan.nextLine();
    if(level1.equals("CLEARED")) {
    System.out.println("Please wait for second level of interview.");
    }else {
    System.out.println("We're Sorry, better luck next time.");
    }

    System.out.println("Have you CLEARED second level of interview?");
    String level2 = scan.nextLine();
    if(level2.equals("CLEARED")) {
    System.out.println("Please wait for third level of interview.");
    }else {
    System.out.println("We're sorry, better luck next time.");
    }

    System.out.println("Have you CLEARED third level of interview?");
    String level3 = scan.nextLine();
    if(level3.equals("CLEARED")) {
    System.out.println("Please wait for fourth level of interview.");
    }else {
    System.out.println("We're sorry, better luck next time.");
    }

    System.out.println("Have you CLEARED fourth level of interview?");
    String level4 = scan.nextLine();
    if(level4.equals("CLEARED")) {
    System.out.println("Kindly wait for final level of HR Interview.");
    System.out.println("Thank you for your patience.");
    }else {
    System.out.println("We're sorry, better luck next time.");
    }

    System.out.println("Have you DONE with your HR Interview?");
    String levelHR = scan.nextLine();
    if(levelHR.equals("DONE")) {
    System.out.println("Please wait in the office lobby, will get back shortly.");
    }

    } else {
    System.out.println("We're Sorry, better luck next time");
    }

    scan.close();
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Nested if esle_Statement not working as desired.

    Please copy the contents of the console from when the program is executed and paste it here.
    Add some comments to the output where you think it is going wrong.

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

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

    to get highlighting and preserve formatting.


    executing the next S.O.P display
    Yes, the code will continue executing with the statement following the else.
    Perhaps you need to continue nesting statements within the true part of the if statement.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. working with data in nested loops
    By Newcomer1 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 3rd, 2022, 09:22 AM
  2. Not getting desired result
    By cybergalaxy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2019, 09:42 AM
  3. [SOLVED] Throw and catches not working as desired
    By kukupie123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2019, 09:49 AM
  4. How is this nested for loop working!
    By samadniz in forum Member Introductions
    Replies: 2
    Last Post: September 3rd, 2012, 09:59 AM
  5. HELP-WHY THE O/P IS NOT AS DESIRED.???
    By shreyash37 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 20th, 2010, 02:45 PM