Note that there are some explanatory texts on larger screens.

plurals
  1. POChoose random String from two dimensional array?
    primarykey
    data
    text
    <p>So what I have is a 2d array with the name of the drink, and the price Test[Name][Price]:</p> <pre><code> public static final String[][] Test = {{"vodka1","5.0"},{"vodka2","10.0"},{"vodka3","15.0"},{"vodka4","20.0"},{"vodka5","25.0"}}; </code></pre> <p>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.</p> <p>So first of all how do i narrow down the array to just the drinks that are lower than the users max price?</p> <p>This is what i tried ( I know its wrong but its all i could think of):</p> <pre><code> private static final String[] test1 = {}; test1 = (array_city.Test[i][j] &lt;= Price); randomIndex = random.nextInt(test1.length); text2.setText(test1[randomIndex]); </code></pre> <p>Thanks!</p> <h1>EDIT</h1> <p>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:</p> <pre><code> convert = Double.valueOf(array_city.Test[c][1]); // Set vodka brand while(Price &lt;= 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]); </code></pre> <p>Why does this not work?</p> <h1>FINAL EDIT</h1> <p>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:</p> <pre><code> convert = Double.valueOf(array_city.Test[c][1]); // Set vodka brand for(double i = Price; i &gt;= convert;){ c++; convert = Double.valueOf(array_city.Test[c][1]); } final TextView text2 = (TextView) findViewById(R.id.display2); randomIndex = random.nextInt(c); </code></pre>
    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.
    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