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

Thread: Looping Array and Satisfying multiple conditions-Support

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

    Default Looping Array and Satisfying multiple conditions-Support

    If I pass the url=http/market/state/update,I need the output as MARKET_STATE_UPDATE but now I am getting MARKET _UPDATE.
    please share your thoughts to help me to solve the issue


    String[] tokens = requestUrl.getPath().split("/", -1);
    for (String tokens1 : tokens) {
    switch (tokens1)
    case “market”:
    if (requestType.equals("put")) { //satisfying this condition
    if (tokens.equals("state")) { // when it comes to this line its not reading the state value
    return RecordDto.EventTypeEnum.MARKET_STATE_UPDATE;
    } else if (tokens.equals("chain”)) {
    return RecordDto.EventTypeEnum.MARKET_CHAIN_UPDATE;
    } else {
    return RecordDto.EventTypeEnum.MARKET_UPDATE; // In the end i am getting this result
    }
    } else if (requestType.equals("delete")) {
    if (tokens.equals("chain”)) {
    return RecordDto.EventTypeEnum.MARKET_CHAIN_DELETE;
    }
    break;
    Case “value”:
    if (requestType.equals("put")) {
    if (tokens.equals(“level”)) {
    return RecordDto.EventTypeEnum.Value_LEVEL_UPDATE;
    } else if (tokens.equals(“period”)) {
    return RecordDto.EventTypeEnum.Value_PERIOD_UPDATE;
    } else {
    return RecordDto.EventTypeEnum.VALUE_UPDATE;
    }
    } else if (requestType.equals("delete")) {
    if (tokens.equals(“period”)) {
    return RecordDto.EventTypeEnum.VALUE_PERIOD_DELETE;
    }
    }
    break;
    Last edited by jashvin; January 26th, 2022 at 03:57 AM.

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

    Default Re: Looping Array and Satisfying multiple conditions-Support

    I am testing this url in the postman. state is an subcategory of market. If user update in the market/{id} value then it should report them market_update likewise if they changed in the state part then it should inform them as your market_state value is updated.

  3. #3
    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: Looping Array and Satisfying multiple conditions-Support

    url=http/market/state/update
    That is not a valid URL. The protocol at the start should be http://

    if (tokens.equals("state"))
    What is that if statement supposed to do? It is comparing an array to a String.
    That will never be true.



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

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

    to get highlighting and preserve formatting.

    Also posted here: https://coderanch.com/t/749250/java/...itions#3478981

    Please read:http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    jashvin (January 26th, 2022)

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

    Default Re: Looping Array and Satisfying multiple conditions-Support

    Will you please elaborate more. The url is not a problem.

    What is that if statement supposed to do? It is comparing an array to a String.
    That will never be true

    In this case.How we can solve the issue?

  6. #5
    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: Looping Array and Satisfying multiple conditions-Support

    How we can solve the issue?
    What is that statement supposed to do?
    You need a description to what that code is supposed to do to be able to write the code to do it.

    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.

  7. #6
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Looping Array and Satisfying multiple conditions-Support

    It seems like there are a few issues in your code that are causing unexpected behavior. Let's address them:

    1. Incorrect comparison: In your code, you're using `tokens.equals("state")` and similar comparisons. However, `tokens` is an array, so you should compare its elements (`tokens1.equals("state")`). Similarly, you should adjust other comparisons accordingly.

    2. Switch-case syntax error: Your switch-case block is missing the braces `{}` and also the `break` statement. Each case should be enclosed within braces, and a break statement should be added at the end of each case to prevent fall-through.

    Here's the corrected version of your code:

    ```java
    String[] tokens = requestUrl.getPath().split("/", -1);
    for (String tokens1 : tokens) {
    switch (tokens1) {
    case "market":
    if (requestType.equals("put")) {
    if ("state".equals(tokens[2])) { // Check if "state" is the third token
    return RecordDto.EventTypeEnum.MARKET_STATE_UPDATE;
    } else if ("chain".equals(tokens[2])) { // Check if "chain" is the third token
    return RecordDto.EventTypeEnum.MARKET_CHAIN_UPDATE;
    } else {
    return RecordDto.EventTypeEnum.MARKET_UPDATE;
    }
    } else if (requestType.equals("delete")) {
    if ("chain".equals(tokens[2])) { // Check if "chain" is the third token
    return RecordDto.EventTypeEnum.MARKET_CHAIN_DELETE;
    }
    }
    break; // Break after each case
    case "value":
    if (requestType.equals("put")) {
    if ("level".equals(tokens[2])) { // Check if "level" is the third token
    return RecordDto.EventTypeEnum.VALUE_LEVEL_UPDATE;
    } else if ("period".equals(tokens[2])) { // Check if "period" is the third token
    return RecordDto.EventTypeEnum.VALUE_PERIOD_UPDATE;
    } else {
    return RecordDto.EventTypeEnum.VALUE_UPDATE;
    }
    } else if (requestType.equals("delete")) {
    if ("period".equals(tokens[2])) { // Check if "period" is the third token
    return RecordDto.EventTypeEnum.VALUE_PERIOD_DELETE;
    }
    }
    break; // Break after each case
    }
    }
    ```

    In this corrected version, I've adjusted the comparisons to check against specific elements of the `tokens` array (`tokens[2]`), which represent the third part of the URL path. Also, I've added braces `{}` around each case and included `break` statements to ensure proper flow control within the switch-case block. If you find yourself needing further help with Java assignment or programming homework, exploring additional resources and seeking guidance from experienced professionals can be beneficial like programminghomeworkhelp.com.

Similar Threads

  1. LSP: weaken pre-condition and strengthen post-condition
    By Capital in forum Object Oriented Programming
    Replies: 1
    Last Post: July 13th, 2014, 12:46 PM
  2. For Loop with condition
    By mindful314 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 11th, 2014, 04:00 PM
  3. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 02:23 PM
  4. Array and Loop?
    By Kristenw17 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 13th, 2012, 04:58 AM
  5. For loop in array
    By Mickeydus in forum Loops & Control Statements
    Replies: 2
    Last Post: March 26th, 2012, 02:37 PM