Home > Mobile >  How to store all the user inputs in do while loop, and call them to print later in Java program?
How to store all the user inputs in do while loop, and call them to print later in Java program?

Time:12-22

I have to write a Java program with do while loop. Basically it's like self-checkout register in grocery stores. At first program prompts user to enter Item 1 name and price, after we enter it asks "do you want to add more?" and prompts user. if we say yes then it repeats, add Item 2 (item number increments) name and price. So it goes like this till we say "no" or till entering 10 items. And after that program prints all item numbers correspondingly their names, and prices one by one. Also calculates total of all items. My code prints only last item number, name, price and total. I don't know how to merge array to do while. Example how it should look:

_output: Enter Item1 and its price:_

_input: Tomatoes_

_input: 5.5_

_output: Add one more item?_

_input: yes_

_output: Enter Item2 and its price:_

_input: Cheese_

_input: 3.5_

_output: Add one more item?_

_input: yes_

_output: Enter Item3 and its price:_

_input: Apples_

_input: 6.3_

_output: Add one more item?_

_input: no_

_output: Item1: Tomatoes Price: 5.5, Item2: Cheese Price: 3.5, Item3: Apples Price: 6.3_

_output: Total price: 15.3_

Here's my code. It prints only last item's number, name and price with total. But can't manage to print all of them one by one as in example.

        Scanner scan = new Scanner(System.in);
        
         String item;
        double price;
        int i=1;
        double total=0;
        String quest;
        String items [] = {}; 
        do {
            System.out.println("Enter item "   i   " and it's price:");
            item = scan.next();
            price=scan.nextDouble();
            i  ;
            total=total price;
           
           
           
            System.out.println("Add one more item?");
            quest=scan.next();
        } while (quest.equalsIgnoreCase("yes")) ; {
            System.out.println("Item " i ": "   item   " Price: "   price   ",");
            System.out.println("Total price: "   total);
        }

CodePudding user response:

You need to store each input in an array or collection, such as ArrayList.

If you choose to use an array, you can ask the user how many items will be entered then use that value for the length of the array:

System.out.print("Enter the number of items in your cart: ");
int arraySize = scan.nextInt();

String[] itemNames = new String[arraySize];
double[] itemPrices = new double[arraySize];

If you decide you use a list, you do not need to ask the user for the list size, since lists are dynamically expanding data types:

List<String> itemNames = new ArrayList<String>();
List<Double> itemPrices = new ArrayList<Double>();

Then for every iteration of your loop, you save the values into the arrays/lists:

System.out.print("Enter the number of items in your cart: ");
int arraySize = scan.nextInt();

String[] itemNames = new String[arraySize];
double[] itemPrices = new double[arraySize];

for(int i = 0; i < ararySize; i  ) {
    System.out.println("Enter item "   (i   1)   " and it's price:");

    itemNames[i] = scan.next();
    itemPrices[i] = scan.nextDouble();

    // ...    
}
// ...

Or for the list approach:

List<String> itemNames = new ArrayList<String>();
List<Double> itemPrices = new ArrayList<Double>();

do {
    System.out.println("Enter item "   i   " and it's price:");

    itemNames.add(scan.next());
    itemPrices.add(scan.nextDouble());

    // ...
} while (quest.equalsIgnoreCase("yes")); {
    // ...
}

Then you can iterate over the list/array to print your output:

for(int i = 0; i < itemNames.length; i  ) {
    String name = itemNames[i];
    double price = itemPrices[i];
    System.out.println("Item "   i   ": "   name   " Price: "   price   ",");
}

For list:

for(int i = 0; i < itemNames.size(); i  ) {
    String name = itemNames.get(i);
    double price = itemPrices.get(i);
    System.out.println("Item "   i   ": "   name   " Price: "   price   ",");
}

CodePudding user response:

I finally solved the task. Thanks everybody who helped, really appreciated. Here's the code:

  Scanner scan = new Scanner(System.in);
  String itemName;
  double price;
  String [] items = new String [10] ;
  double [] prices = new double [10];
  int itemNumbers[]=new int [10];
  String answer;
  double total=0;
  int i=1;
  
  int index=0;
  do {
      System.out.println("Enter item "   i   " and it's price:");
      itemName=scan.next();
      price=scan.nextDouble();
      scan.nextLine();
      total=total price;
      items[index]=itemName;
      prices[index]=price;
      itemNumbers[index]=i;
      index  ;
      System.out.println("Add one more item?");
      answer=scan.next();
      i  ;
      
  } while (answer.equalsIgnoreCase("yes") && i<=10) ; {
      for (int j=0; j<index; j  ) {
          System.out.print("Item "  itemNumbers[j]  ": "   items[j]   " price: "   prices[j]   ",");
      }
  }
      System.out.println("\nTotal price: "   total);
}

}

CodePudding user response:

This version works too. Without arrays.

String shoppingList = "";
            String item = "";
            String addMore = "";
            double price = 0;
            int count = 1;
            double totalPrice = 0;

            do {
                System.out.println("Enter Item"  count   " and its price:");
                item = scan.next();
                price = scan.nextDouble();
                shoppingList  = ("Item"   count   ": "   item   " Price: "   price   ", ");
                totalPrice  = price;
                System.out.println("Add one more item?");
                addMore = scan.next();
                count  ;
            } while (addMore.equalsIgnoreCase("yes"));
            System.out.println(shoppingList.substring(0, shoppingList.length()-2));
            System.out.println("Total price: "   totalPrice);
        
  • Related