0

So what I'm trying to do is take input from user for two different products being bought. I need to have this data inputted into the table that I believe I'm creating correctly. However the scanner method is only keeping the information but not outputting them into the table. My question really is can you create sub variables for the previously declare variable such as p1 and p2 for the variable productName or qty1 and qty2 for quantity. I know why its not outputting data into the table because I incorrectly labeled the placeholders for example %f. I haven't found any information saying so and from everything I've found that is closely related would be creating an array for all the inputs needed. I'm not the best but I would appreciate any help. Thanks

import java.util.Scanner;
import java.text.NumberFormat;

public class Project2
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int quantity;
        double unitPrice;
        String productName;

        System.out.println("Enter name for Product 1:");
        productName = scan.nextLine();

        System.out.println("Enter the amount desired for Product 1:");
        quantity = scan.nextInt();

        System.out.println("Please enter the price of Product 1:");
        unitPrice = scan.nextDouble();

        double total = unitPrice * quantity;

        System.out.println("Enter name for Product 2:");
        productName = scan.nextLine();

        System.out.println("Enter the amount desired for Product 2:");
        quantity = scan.nextInt();

        System.out.println("Please enter the price of Product 1:");
        unitPrice = scan.nextDouble();

        System.out.println("--------------------------------------------");
        System.out.println("|   Product |    Qty |   Price |   Total   |");
        System.out.println("--------------------------------------------");
        System.out.printf("| %c        |  %f    |   %d    |   %.2f    |\n",productName, quantity,unitPrice, total);
        System.out.printf("| %c        |  %f    |   %d    |   %.2f    |\n",productName, quantity, unitPrice, total);
        System.out.println("--------------------------------------------    ");
    }
}
Alexander Mikhalchenko
  • 4,315
  • 3
  • 28
  • 54
  • What is a "sub variable"? – azurefrog Feb 06 '16 at 18:44
  • bad wording on my part its not called that... I'm basically trying to create a variable that's linked or part of the variables declared already. So lets say I wanted to created p1 for Product one but it be stored in productName. I guess what I'm trying to do is separate the information asked between product 1 and 2. – Average Joe Feb 06 '16 at 18:46
  • They are just called variables and that is exactly what you are doing – Idos Feb 06 '16 at 18:47
  • If you want two products you'll need two product names and two of each other variable (or a loop). But you'll still have the issue of skipping the nextLine. A reference variable refers to one thing, a collection can refer to multiple reference variables. It's unclear what you really want here. Maybe a `List` of `Product` POJOs. – Elliott Frisch Feb 06 '16 at 18:49
  • @ElliottFrisch I believe that was what I am missing. Baiscally whats being required of me is both product names need to be stored under productName, quantity for both products, price in unitPrice, and total. But like I said all the input asked has to be stored in those 3 variables and that is what I was missing because when I try to output in the table it does the most recent scan capture. But thank you I think I know where I need to go from here. – Average Joe Feb 06 '16 at 18:57

1 Answers1

0

You should create two variables for each property. If you use one variable for each property, you will end up losing the values for product one.

iwsnmw
  • 466
  • 1
  • 3
  • 10