Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:34:58+00:00 2026-05-29T11:34:58+00:00

I am having issues with the following part of my code. when nn is

  • 0

I am having issues with the following part of my code.
when “nn” is entered i get invalid code.
when valid code is entered i get invalid code however this only happens once.
program doesn’t seem to work as intended. Please assist.

    System.out.println("ENTER CODE (nn to Stop) : ");
    ArrayList<Product> list = new ArrayList<Product>();
    .
    .
    .
    .


    ArrayList<Code> codeList = new ArrayList<Code>();


    for (Product product : list) {
        System.out.print("CODE : ");
        String pcode = scan.next();
        if (pcode.equalsIgnoreCase("nn")) {
            break;
        }

        if (!(code.equalsIgnoreCase(product.getCode()))) {
            System.out.println("Invalid code, please enter valid code.");
            System.out.print("CODE : ");
            pcode = scan.next();

        }

        System.out.print("QUANTITY : ");
        int quan = scan.nextInt();
        while (quan > 20) {
            System.out.println("Purchase of more than 20 items are not allowed, please enter lower amount.");
            System.out.print("QUANTITY : ");
            quan = scan.nextInt();
        }
        codeList.add(new Code(pcode, quan));
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:34:58+00:00Added an answer on May 29, 2026 at 11:34 am

    You want continue instead of break.

    Also, you should only call code = scan.next() once inside the loop; otherwise you’ll skip over some items.

    String code = scan.next();
    boolean match = false;
    for (Product product : list) {
        if (code.equalsIgnoreCase(product.getCode())) {
            match = true;
            break;
        }
    }
    // now only if match is false do you have an invalid product code.
    

    Update:

    I still can’t get this to work. What I am trying to do is test user
    input to make sure that product code exists, if not prompt that the
    product code entered is invalid and asks for correct code. I also need
    to have the condition to stop order when “nn” is entered. I have tried
    while loops, do-while loops etc. i can’t seem to get it right. Please
    assist. My problem is with writing code for multiple conditions. When
    one is working correctly the other isn’t.

    while (true) {
        final String code = scan.next();
        if (isExitCode(code)) {
            break;
        }
        if (!isValidCode(code)) {
            System.out.println("Invalid code, please enter valid code.");
            continue;
        }
        int quantity = -1;
        while (true) {
            quantity = scan.nextInt();
            if (!isValidQuantity(quantity)) {
                System.out.println("bad quantity");
                continue;
            }
            break;
        }
        // if you've got here, you have a valid code and a valid 
        // quantity; deal with it as you see fit.
    }
    

    Now you just need to write the methods isExitCode(), isValidCode(), and isValidQuantity().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am having issues with the following and it crashes the iPhone simulator,
Following on from my earlier question , I am still having issues with loading
I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using
Ok. I'm having an issue with the following bit of code: StreamReader arrComputer =
I'm having an issue with rails involving javascript. Basically, I have the following code:
I am trying to run an existing python code, and having issues with it.
I have the following vba code is part of a larger script. The issue
I am having following issue with reading binary file in C. I have read
I'm having the following issue in a WinForms app. I'm trying to implement Hotkeys
I'm designing a mySQL DB and I'm having the following issue: Say I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.