Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use arrays in this java code so I can output values in order they are assigned to positions ?
    primarykey
    data
    text
    <p>I am a java beginner and I am coding a program that gets the name of the item, units of that item, price/unit and the total price. I am trying to put those values in different arrays so I can access them later when I create a sort of a receipt but I don't know how to assign those values in array positions and then access those same positions without having to hard-code. A loop is the best option but I dont know how to set this thing up. Help and suggestions would really be appreciated. Keep in mind that I cant do advanced stuff like matrices and 3D arrays. If you can keep it simple it would be awesome.</p> <p>This is the main class, I have a tester class with main() that runs userInput() and menu() but theres no point in putting that in because its only 2 lines of code.</p> <pre><code>import java.util.Scanner; public class GroceryList { // instance variables Scanner Price, Items, NumItems, Units; private double myGross, myNet; private final double STATETAX; private double totalPrice, unitPrice; private String itemName; private int totalUnits; ///////////////////////////////////////////// arrays I will use private double[] totalPrice1; private double[] unitPrice1; private String[] itemName1; private int[] totalUnits1; public GroceryList() { STATETAX = 0.06; double[] totalPrice = new double[50]; double[] unitPrice = new double[50]; String[] itemName = new String[50]; int[] totalUnits = new int[50]; } public void userInput() { Scanner Price = new Scanner(System.in); Scanner Items = new Scanner(System.in); Scanner Units = new Scanner(System.in); Scanner NumItems = new Scanner(System.in); int u, c, totalItems;// c is the number of items that has to equal totalItems in order for the loop to break double price; String item;//gets the name of the item c=0; System.out.println("Welcome to Grocery List ! \n"); System.out.print("Enter the total number of items you want to buy (not total units !) : "); totalItems = NumItems.nextInt(); System.out.println(); do { c++ ; System.out.print("Enter the item name : "); item = Items.nextLine(); System.out.print("Enter the units of " + item + " you want to buy : "); u = Units.nextInt(); System.out.print("Enter the price of a/n " + item + " : $"); price = Price. nextDouble(); /*this would make only one value appear at the receipt, which would be only one item name, one unit price, one price/unit, one total price and the other values calculated would not appear on the receipt because you cant assign more than 1 value to a variable so thats why I need arrays. */ itemName = item; totalUnits = u; unitPrice = price; calc(u,price,item); } while (c &lt; totalItems); } public void calc(int u, double p, String i) { double total;//total amount of $ for 1 item total = u*p; System.out.println("Total price of " + i + " : $" + total + "\n"); grossPay(total); totalPrice = total; } public void grossPay(double total) { double gross; myGross += total; } public double tax() { double temp; temp = myGross*STATETAX; myNet = myGross - temp; return myNet; } public void menu() { System.out.printf("%-10s %6s %11s %11s" , "ITEM :" , "UNITS :" , "PRICE :" , "TOTAL :"); System.out.println(); System.out.printf("%-11s %2d %7s $%4.2f %5s $%2.2f", itemName, totalUnits,"", unitPrice,"", totalPrice); System.out.println(); } public void payment() { System.out.println("Amount before tax : $" + myGross); System.out.println("Amount after tax : $" + tax()); } }//end GroceryList </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload