0

Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays. I am trying to sort tacos. The user must enter the names of the 10 taco and the prices and I sort them in ascending order.

so far I have:

public class TacoSorter {

public static void main(String[] args) {
    // TODO Auto-generated method stub

Scanner keyboard = new Scanner(System.in);

System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");
System.out.println("Enter the name of taco ");

Scanner input = new Scanner(System.in);

final int TotalTacos = 10;

double[]tacos = new double[TotalTacos];


for(int n =0; n < tacos.length; n++){
    System.out.println("Enter a name of taco" + (n+1));

    tacos[n] = input.nextInt();

    System.out.println("Enter the price of the taco");
}
}

I want the result to be somewhat similar to elcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!

Enter the name of taco 1
Crunchy Taco
Enter taco's price
1.19
Enter the name of taco 2
Crunchy Taco Supreme
Enter taco's price
1.59
Enter the name of taco 3
Soft Taco
Enter taco's price
1.19
Enter the name of taco 4
Soft Taco Supreme
Enter taco's price
1.59
Enter the name of taco 5
Chicken Soft Taco
Enter taco's price
1.79
Enter the name of taco 6
Crispy Potato Soft Taco
Enter taco's price
0.99
Enter the name of taco 7
Double Decker Taco
Enter taco's price
1.89
Enter the name of taco 8
Double Decker Taco Supreme
Enter taco's price
2.29
Enter the name of taco 9
Doritos Locos Taco (Nacho Cheese)
Enter taco's price
1.49
Enter the name of taco 10
Doritos Locs Tacos(Fiery) Supreme
Enter taco's price
1.89

Sorted Tacos are

Taco Prices Crispy Potato Soft Taco 0.99
Taco Prices Crunchy Taco 1.19
Taco Prices Soft Taco 1.19
Taco Prices Doritos Locos Taco (Nacho Cheese) 1.49
Taco Prices Crunchy Taco Supreme 1.59
Taco Prices Soft Taco Supreme 1.59
Taco Prices Chicken Soft Taco 1.79
Taco Prices Double Decker Taco 1.89
Taco Prices Doritos Locs Tacos(Fiery) Supreme 1.89
Taco Prices Double Decker Taco Supreme 2.29
Finally:
wvdz
  • 16,251
  • 4
  • 53
  • 90
  • 1
    possible duplicate of [How do I create an array that organizes the taco prices in ascending order along with its taco name?](http://stackoverflow.com/questions/32680117/how-do-i-create-an-array-that-organizes-the-taco-prices-in-ascending-order-along) – Gnarlywhale Sep 21 '15 at 22:16

1 Answers1

0

Given an array of double values, lets say

double dArr[] = {0.99, 1.19, 1.19, 1.49, 1.59, 1.59, 1.79, 1.89, 1.89, 2.29};

You can sort these values by saying

Arrays.sort(dArr);

As a suggestion, since you have text directly associated to each price value, you have created a perfect scenario for key-value pairings. I do not suggest using just an array and sorting by the price because now you have to add additional logic to associate the price with that particular price description. For these reasons I suggest you use a HashMap, for example.

Sorting a HashMap is just as convenient, just use Collections.sort() method. Cheers!

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136