Note that there are some explanatory texts on larger screens.

plurals
  1. POFilling array of objects from the user's input in java
    primarykey
    data
    text
    <p>I'm trying to make a java application that's allows the user to make a catalog for a supermarket, and then display all the products that the user entered to the catalog. Now I have a problem with filling the array of objects the should be fill in by the user's input. The output should be like the following (user input in <strong>Bold</strong>):</p> <p>Enter product description (or # to stop): <strong>Condensed Powdered water</strong> Enter product code: <strong>P3487</strong> Enter product unit price: <strong>$2.50</strong> Enter product unit phrase: <strong>per packet</strong></p> <p>Enter product description (or # to stop): <strong>Distilled Moonbeams</strong> Enter product code: <strong>K3876</strong> Enter product unit price: <strong>$3.00</strong> Enter product unit phrase: <strong>a dozen</strong></p> <p>Enter product description (or # to stop): <strong>Anti-Gravity Pills</strong> Enter product code: <strong>Z9983</strong> Enter product unit price: <strong>$12.75</strong> Enter product unit phrase: <strong>for 60</strong></p> <p>Enter product description (or # to stop): <strong>#</strong></p> <p>Your catalog: P3487, Condensed Powdered water, $2.50 per packet. K3876, Distilled Moonbeams, $3.00 a dozen. Z9983, Anti-Gravity Pills, $12.75 for 60.</p> <p>The code that I wrote : 2 classes class 1:</p> <pre><code>public class Catalog { private String description ; private String code ; private double price ; private String phrase ; int counter = 0; private Catalog [] list = new Catalog [100]; public Catalog (String productDescription , String productCode , double productPrice , String productPhrase) { description = productDescription; code = productCode; price = productPrice; phrase = productPhrase; } public void setDescription (String productDescription) { description = productDescription; } public String getDescription () { return description; } public void setCode (String productCode) { code = productCode; } public String getCode () { return code; } public void setPrice (double productPrice) { price = productPrice; } public double getPrice () { return price; } public void setPhrase (String productPhrase) { phrase = productPhrase; } public String getPhrase () { return phrase; } </code></pre> <p>class 2:</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CatalogTest { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String name = null; String code = null; double price = 0.0; String phrase = null; BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); Catalog product = new Catalog(name,code,price,phrase); Catalog [] productsArray = new Catalog [100]; for (int i = 0 ; i &lt; productsArray.length ; i ++) { System.out.println("Enter product description (or # to stop): "); name = input.readLine(); if (!("#".equals(name))) { productsArray [i] = product; product.setDescription(name); System.out.println("Enter product code: "); code = input.readLine(); productsArray [i] = product; product.setCode(code); System.out.println("Enter product unit price: "); price = Double.parseDouble(input.readLine()); productsArray [i] = product; product.setPrice(price); System.out.println("Enter product unit phrase: "); phrase = input.readLine(); productsArray [i] = product; product.setPhrase(phrase); productsArray [i] = new Catalog (name,code,price,phrase); } else { System.out.println("Your Catalog:"); System.out.printf("%s, %s,$%.2f %s",product.getCode(),product.getDescription(),product.getPrice(),product .getPhrase()); break; } } } } </code></pre> <p>The output I get (user input in <strong>Bold</strong>) : Enter product description (or # to stop): <strong>condensed powdered water</strong> Enter product code: <strong>p3487</strong> Enter product unit price: <strong>2.50</strong> Enter product unit phrase: <strong>per packet</strong> Enter product description (or # to stop): <strong>distilled moonbeams</strong> Enter product code: <strong>k3876</strong> Enter product unit price: <strong>3</strong> Enter product unit phrase: <strong>a dozen</strong> Enter product description (or # to stop): <strong>#</strong> Your Catalog: k3876, distilled moonbeams,$3.00 a dozen</p> <p>So any help PLEASE ??</p>
    singulars
    1. This table or related slice is empty.
    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.
    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