Note that there are some explanatory texts on larger screens.

plurals
  1. POselection sort algorithm using the array in java
    primarykey
    data
    text
    <p>When the program runs, it asks you to enter a product name and price, and then whenever you press -1 it will be stopped and display a list of the entered product and price. However, the problem is I wrote a selection sort algorithm to sort the list by PRICE in ascending order. The output is not what I expected. Have a look at "//Selection Sort" in this code</p> <pre><code>import java.util.Scanner; public class ProductPrices { private static Scanner keyboard = new Scanner(System.in); private static Scanner key = new Scanner(System.in); final static int arrayLength = 1000; //maximum array size static float[] productPrice = new float[arrayLength]; //stores the prices of products static String[] productName = new String[arrayLength]; //stores the names of products public static void main(String[] args) { System.out.println("SHOPPING. Press -1 to quit anytime.\n"); for(int i=0; i&lt;arrayLength; i++) { System.out.print("Product: "); productName[i] = keyboard.nextLine(); if(productName[i].equals("-1")) break; System.out.print("Price: $"); productPrice[i] = key.nextFloat(); if(productPrice[i] &lt; 0) break; } System.out.println("\nList of the SHOPPING!\n---------------------"); for(int i=0; i&lt;productPrice.length; i++) { if(productName[i] == null || productName[i].equals("-1") || productPrice[i] &lt; 0) continue; // null arrays will not be displayed. else { // Selection sort if(productPrice[i] &gt; productPrice[i+1]) { float temp = productPrice[i]; productPrice[i] = productPrice[i+1]; productPrice[i+1] = temp; } System.out.printf("Item: %s %.2f\n", productName[i], productPrice[i]); } } } } </code></pre> <blockquote> <pre><code> For example :::Input::: Product: apple Price: $2.35 Product: pie Price: $1.36 Product: cereal Price: $7.4 Product: -1 :::Output::: Item: apple 1.36 Item: pie 2.35 Item: cereal 0.00 That is incorrect, it should be Item: pie 1.36 Item: apple 2.35 Item: cereal 7.40 </code></pre> </blockquote>
    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