So what I have is a 2d array with the name of the drink, and the price Test[Name][Price]:
public static final String[][] Test = {{"vodka1","5.0"},{"vodka2","10.0"},{"vodka3","15.0"},{"vodka4","20.0"},{"vodka5","25.0"}};
What im trying to do is have it so that the user inputs their max price and then a drink is randomly chosen from the 2d array that is below their max price.
So first of all how do i narrow down the array to just the drinks that are lower than the users max price?
This is what i tried ( I know its wrong but its all i could think of):
private static final String[] test1 = {};
test1 = (array_city.Test[i][j] <= Price);
randomIndex = random.nextInt(test1.length);
text2.setText(test1[randomIndex]);
Thanks!
EDIT
I have sorted my array into least to greatest according to prices and tried this code in order to find the greatest drink possible to buy, pick a random index somehwere between , and then setText to that string but when the activity page starts it crashes? Here is my code:
convert = Double.valueOf(array_city.Test[c][1]);
// Set vodka brand
while(Price <= convert){
c++;
convert = Double.valueOf(array_city.Test[c][1]);
}
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(c);
text2.setText(array_city.Test[randomIndex][1]);
Why does this not work?
FINAL EDIT
Figured it out!! turned out to be some minor logic issues, changed to a four loop and it works great!! here is what i did to my code:
convert = Double.valueOf(array_city.Test[c][1]);
// Set vodka brand
for(double i = Price; i >= convert;){
c++;
convert = Double.valueOf(array_city.Test[c][1]);
}
final TextView text2 = (TextView) findViewById(R.id.display2);
randomIndex = random.nextInt(c);
[1,2,3,4,5]etc.. and not[3,5,2,1,4].. if the array is always sorted, then you can just ignore the portion above/below the value.