Hi everyone, I am trying figure out is there a "more proper" way to show the multiple lowest values in an array. My array has 2 of the values that are lowest among the 8 values. I would like to know is there a way to show both of them without using:

for (int i = 0; i < price.length; ++i)
		   {
			  if (price[i]<= lowest)
		         {
		          lowest = price[i];
			  loworderNo=i;//make the lowest price order number equals to the i        //value, who is index contains lowest price
		          }
	            }
			 System.out.println("Most Economical Food:\t" + (loworderNo-1) +"\t\t"+ item[loworderNo-1] +  "\t" + lowest); 
			 System.out.println("Most Economical Food:\t" + loworderNo +"\t\t"+ item[loworderNo] +  "\t" + lowest);

Notes: My assignment is about a restaurant order taking software written in java to take order, etc and here is the actual array of the dishes & its price.

String[] itemArray = { "Premium Bento", "Deluxe Bento", "Salmon Teriyaki", "Tempura Ramen","Salmon Sashimi", "Maguro Sashimi", "Maguro Sushi", "Tamago Sushi"  };

	double[] priceArray = { 21.00, 17.00, 12.00, 10.00, 4.90, 4.90, 1.80, 1.80 };

See there is 2 lowest values, 2 of them are $1.80, I could have used the above method, but I just cannot satisfied with it (I think this is call hard coding right??)

Please help. Thank you.

Recommended Answers

All 12 Replies

Arrays class with its sort() methods is all you need

Do you know that the array will already have the values sorted? From what I see:
double[] priceArray = { 21.00, 17.00, 12.00, 10.00, 4.90, 4.90, 1.80, 1.80 } ;
The array is already sorted, so you just need to take the 2 last values.

If this is not the case then writing:

System.out.println("Most Economical Food:\t" + (loworderNo-1) +"\t\t"+ item[[B]loworderNo-1[/B]] + "\t" + lowest);
System.out.println("Most Economical Food:\t" + loworderNo +"\t\t"+ item[loworderNo] + "\t" + lowest);
is not quite correct, because since the array will not be sorted you don't know if the lowest prices will be next to each other.
Plus if the array doesn't have 2 lowest prices but 1 you will have an ArrayIndexOutOfBoundsException.
Check before you access an array if the values is in bounds. You are sure for variable: loworderNo but you don't know 100% that ( loworderNo - 1 ) will be OK.

i tried using

import java.util.Arrays;

so as to use the method Arrays.sort(); but it only shows the highest instead, i would like the 2 lowest value plaease.

Arrays class with its sort() methods is all you need

But if he sorts priceArray then the itemArray will also have to be sorted the same way.
It would be better to use the old fashion way to sort priceArray and do equivalent swapping to itemArray.
Or
Create an object with two attributes:
price, foodName:

class Food {
private String foodName;
private double price;

//add get, set methods OR make the above variables public
}

Then use the interface: Comparable:

class Food implements Comparable{
// code

public int compareTo(Food f) {

}
}

Comparable

Objects that implement Comparable can be used as arguments at the Arrays.sort() method

i tried using

import java.util.Arrays;

so as to use the method Arrays.sort(); but it only shows the highest instead, i would like the 2 lowest value plaease.

What do you mean the highest?
The Arrays.sort(int[] a) sorts the array. It's up to you to take the values you want

Edit: I would suggest to read my previous post

i tried using

import java.util.Arrays;

so as to use the method Arrays.sort(); but it only shows the highest instead, i would like the 2 lowest value plaease.

As it says in API "Sorts the specified array of doubles into ascending numerical order." that mean first two values in array are the lowest. If you look at the end you will obviously get the largest number

Do you know that the array will already have the values sorted? From what I see:
double[] priceArray = { 21.00, 17.00, 12.00, 10.00, 4.90, 4.90, 1.80, 1.80 } ;
The array is already sorted, so you just need to take the 2 last values.

If this is not the case then writing:

System.out.println("Most Economical Food:\t" + (loworderNo-1) +"\t\t"+ item[[B]loworderNo-1[/B]] + "\t" + lowest);
System.out.println("Most Economical Food:\t" + loworderNo +"\t\t"+ item[loworderNo] + "\t" + lowest);
is not quite correct, because since the array will not be sorted you don't know if the lowest prices will be next to each other.
Plus if the array doesn't have 2 lowest prices but 1 you will have an ArrayIndexOutOfBoundsException.
Check before you access an array if the values is in bounds. You are sure for variable: loworderNo but you don't know 100% that ( loworderNo - 1 ) will be OK.

Thank you, i understand what you are trying to mean, yes luckily this assignment was sorted by hand that the price goes in this way, I mean what if that is not the case, and I want to find out a better way to do it. (Otherwise the food name will prompt with wrong price.

Then how should I go for the descending order?

Instead of getting the last two values, take the first two. And don't forget that sorting the priceArray, doesn't sort the itemArray. And if you sort the itemArray, it will be sorted based on the values itemArray has, and the mapping between names and prices will be mixed up.

I did not pay attention to first question, because of missing code tags therefore the Arrays.sort() is not best solution. I would recommend to get price and items in two dimensional array and use sorting algorithm like bubble sort on the price part of the array to get things in desired order

Ok i see, it seemed like it may be a complicated process for an absolute beginner, and i got the concept that Arrays.sort() usually sorts in ascending order and cannot be "interlinked" between arrays. I think I will just have to stick with the old fashion way (a bit hardcoding), thanks to all who have replied.

Ok thanks for the help, 2D arrays are not the focus of my syllabus at this moment, but it is mentioned about multi-dimension array.

Thank you once again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.