Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8488301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:29:36+00:00 2026-06-10T21:29:36+00:00

So what I have is a 2d array with the name of the drink,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:29:38+00:00Added an answer on June 10, 2026 at 9:29 pm

    First of all, instead of making it a String[][] array, you should make it a Drink[] array (where Drink is a class that you have defined and which has a String name, and a float price. This will help you use the info, because you won’t have to constantly worry about parsing the price String to a double.

    Here is a pseudocode solution if the array is sorted by price (from lowest to highest):

    1. First find the most expensive drink that you can still buy. You could binary search for it, but a simpler solution would be to go from index 0 to Test.length-1 checking whether or not the drink would be buyable. If not, you would stop and store the index of the last buyable drink. If it were buyable, you would continue.
    2. Then, you would generate a random int from 0 to the maxIndex (inclusive), and output the Drink.

    For example,

    (int)(Math.random()*(maxIndex+1)) would get you the random integer.

    EDIT

    Since the array is not necessarily sorted, you can sort it. In order to do this, use java.util.Arrays.sort(Object[] o, Comparator c) to sort it.

    Input your Drink[] as the first parameter. And your DrinkComparator as your second. This will quicksort it for you.

    Assuming that your Drink class is defined as follows:

    public class Drink {
    
            String name;
            double price; // You could also use floats
    
            public Drink(String n, double p) {
    
                price = p;
                name = n;
            }
        }
    

    You can make your DrinkComparator class like this.

    http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Comparator.html

        class DrinkComparator implements Comparator {
    
             public int compare(Object o1, Object o2) {
                 if(o1.price < o2.price) {
                     return 1;
                 }
                 else if(o1.price == o2.price) { // Disregarding float imprecision
                     return 0;
                 }
                 else { // Not necessary, but here for the sake of readability.
                     return -1;
                 }
             }
    
             public boolean equals(Object o) { // I don't think you will be using this method.
                 return true; // If you run into problems, tell me.
             }
        }
    }
    

    Then you would sort like this:

    Arrays.sort(drinks, new DrinkComparator());

    drinks would be your Drink[] of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an array of Tag objects class Tag { public string Name; public
I have a array like this in a function: $value = array(name=>test, age=>00); I
I have this array: array (size=3) 0 => array (size=2) 'name' => string 'XML'
I have an array generated by PHP array( array( 'name'=>'node1', 'id' => '4' ),
i have an array: Array ( [47] => Array ( [name] => 3543 good
I have an array that has keys and values. For eg: Array ( [name]
Let's say I have this array, $array = array( 'name' => 'hermet', 'emails' =>
I have an array with name $name[] and another one with the name $content[]
i have this JSON: var projects_array = new Array( {name:myName1, id:myid1, index:1}, {name:myName2, id:myid2,
I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.