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 9169785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:55:36+00:00 2026-06-17T15:55:36+00:00

I am writing a shop simulator for homework. A user inputs items for sale

  • 0

I am writing a shop simulator for homework. A user inputs items for sale (set name and price) and then the user buys these items – entering ID (1-5) and count. And then – price counting (my problem).

It must be done simply, but I can’t find what is wrong. The final price has strange values, and I can’t understand why.

In this code I added some lines of “debugger” code, that show intermediate numbers – for best understanding of process.

import java.util.Scanner; 

public class HomeWork3Shop {

    private static Scanner inputAdmin;

    public static void main(String[] args) {

        String[] items = new String[6];
        int[] price = new int[6];

        // The administrator adds the information about the products
        System.out.println("Administrator: add five items - name and price: ");
        for (int i = 1; i < 6; i++) {
            // if int = 0 -- will be "item 0: xxx" - not good
            System.out.print(" item " + i + ": ");
            inputAdmin = new Scanner(System.in);
            items[i] = inputAdmin.next();
            System.out.print("Price " + i + ": ");
            inputAdmin = new Scanner(System.in);
            price[i] = inputAdmin.nextInt();

        }

        int[][] buyList = new int[2][6];
        String yn = null;
        System.out.print("\nAdded. Plese buy - enter ID of item (1-5): ");

        int i = 1;
        for (int i2 = 0; i2 < 5; i2++) {
            // Enter ID of item:
            Scanner inputShoper = new Scanner(System.in);
            buyList[0][i] = inputShoper.nextInt();
            // Insert ID of item to the array - for next price count

            System.out.print("How much? (Enter a number): ");
            buyList[1][i++] = inputShoper.nextInt();
            System.out.print("\nIn bag. Want buy more? [y/n] ");

            Scanner YN = new Scanner(System.in);
            yn = YN.next();
            if (yn.equals("n")) {               
                break;
            }

            System.out.print("Enter ID of next item to buy: ");

        }


        for (int row = 0; row < buyList.length; row++) {
            // paint a table
            for (int col = 0; col < buyList[row].length; col++) {               
                System.out.print(buyList[row][col] + "\t");                   
            }             
            System.out.println();            
        }


        for (int temp = 0; temp < items.length; temp++) {              
            System.out.print(" " + items[temp]);                
        }

        for (int temp = 0; temp < items.length; temp++) {                
            System.out.print(" " + price[temp]);               
        }

        // ----- price count
        int totalPrice = 0;
        int tempPrice = 0;
        for (i = 1; i < buyList[0].length; i++) {                
            tempPrice = buyList[1][i] * price[i];
            System.out.print(" | " + tempPrice);
            totalPrice += buyList[1][i] * price[i];
            System.out.println(totalPrice);
            // count * price             
        }

        System.out.println("Your price is: " + totalPrice);

        // ----- black list -----
        System.out.print("How much money you have? ");

        int cash = 0;
        Scanner Cash = new Scanner(System.in);
        cash = Cash.nextInt();

        if (cash < totalPrice) {                
            System.out.println("You are in our Black List.");            
        }
        else {                
            System.out.println("Thank you for purchasing.");                
        }

    }

}

Output:

 Administrator: add five items - name and price: 
     item 1: Milk
    Price 1: 11
     item 2: Broad
    Price 2: 22
     item 3: Mouse
    Price 3: 33
     item 4: Keyboard
    Price 4: 44
     item 5: Monitor
    Price 5: 55

    Added. Plese buy - enter ID of item (1-5): 1
    How much? (Enter a number): 1

    In bag. Want buy more? [y/n] y
    Enter ID of next item to buy: 2
    How much? (Enter a number): 2

    In bag. Want buy more? [y/n] y
    Enter ID of next item to buy: 5
    How much? (Enter a number): 4

    In bag. Want buy more? [y/n] n
    0 1   2   5   0   0   
    0 1   2   4   0   0   
     null Milk Broad Mouse Keyboard Monitor 0 11 22 33 44 55 | 1111
     | 4455
     | 132187
     | 0187
     | 0187
    Your price is: 187

Final price – 187. But:

(11*1) + (22*2) + (55*4) = 22 + 44 + 220 = 286.

286 – must be, but this code get 187.

Its calculate in this line of code:

totalPrice += buyList[1][i] * price[i];
  • 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-06-17T15:55:37+00:00Added an answer on June 17, 2026 at 3:55 pm

    There is an error in the calculation loop. You need to access the price of the item you are counting price[buyList[0][1]] instead of price[i], use this instead:

    for (i = 1; i < buyList[0].length; i++) {
        tempPrice = buyList[1][i] * price[buyList[0][i]];
        System.out.print(" | " + tempPrice);
        totalPrice += buyList[1][i] * price[buyList[0][i]];
        System.out.println(totalPrice);
        // count * price
    }
    

    Apart from this, as suggested, do not create a Scanner every time, also, you already have the price in the tempPrice variable, you should not calculate that 2 times (line 2 and 4 in this code)

    Hope it helps.

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

Sidebar

Related Questions

I'm writing a website similar to Amazon. Each user can open a shop. Thus
I am writing a small e-shop application with Symfony 2 and I need some
Writing/reading code seems less stress then preparing a deploy scripts such as ./configure then
I am writing a little shop administration software for my uncles fishing shop. It
wi'm writing an online shop and while i was modelling the domain a question
Im writing my first online shop in PHP. Now working on add product form
Ok so I am writing a game that has a shop. The shop is
I'm writing a ticket-shop-system atm for reserving tickets for various concerts. What I have
I am writing a shop application, and I have a question. As you know
Writing a dictionary application for android. Want to set translation direction in accord with

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.