Home > Enterprise >  How to find the maximum value double in an array
How to find the maximum value double in an array

Time:10-22

I'm building a calculator to get a price for a medical marijuana dispensary. There is a 16 percent tax on all sales, as well as a price break at certain weights. My goal is to find a weight to sell at the best price point for the customer, given tax and the break. Im down to just trying to get the highest value of a list of doubles, to compare to the breakpoint array to see if they meet the certain threshold for the breakpoint. I literally just cant seem to get the maximum double in the weightList array. Here is my code. My doubles gramPrice, eighthPrice, etc are the price/gram at a given breakpoint. The gramWeight, eighthWeight, etc are the weight of product calculated to recieve at that breakpoint. Kinda sloppy terminology I guess, but how can I find the maximum value of my double array?

import java.util.Scanner;
import java.text.DecimalFormat;
public class main {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        DecimalFormat df = new DecimalFormat("##.##");
        
        int desiredPrice = 0 , pricePoint = 0;
        int fivePerGram[] = {5, 15, 25, 50, 100};
        int eightPerGram[] = {8, 25, 45, 90, 160};
        double weightFormat[] = {1, 3.5, 7, 14, 28};
        
        System.out.print("Enter Desired Amount OTD: ");
        if(reader.hasNextInt() == true) {
            desiredPrice = reader.nextInt();
        }
        System.out.print("Enter Price Point (3, 5, 8, 10, 12): " );
        if(reader.hasNextInt() == true) {
            pricePoint = reader.nextInt();
        }
        
        if(pricePoint == 8) {
            double gramPrice = (eightPerGram[0]/1)*1.16;gramPrice = Double.parseDouble(df.format(gramPrice));
            double eighthPrice = (eightPerGram[1]/3.5)*1.16;eighthPrice = Double.parseDouble(df.format(eighthPrice));
            double quarterPrice = (eightPerGram[2]/7)*1.16;quarterPrice = Double.parseDouble(df.format(quarterPrice));
            double halfPrice = (eightPerGram[3]/14)*1.16;halfPrice = Double.parseDouble(df.format(halfPrice));
            double ouncePrice = (eightPerGram[4]/28)*1.16;ouncePrice = Double.parseDouble(df.format(ouncePrice));
            
            double gramWeight = desiredPrice/gramPrice;gramWeight = Double.parseDouble(df.format(gramWeight));
            double eighthWeight = desiredPrice/eighthPrice;eighthWeight = Double.parseDouble(df.format(eighthWeight));
            double quarterWeight = desiredPrice/quarterPrice;quarterWeight = Double.parseDouble(df.format(quarterWeight));
            double halfWeight = desiredPrice/halfPrice;halfWeight = Double.parseDouble(df.format(halfWeight));
            double ounceWeight = desiredPrice/ouncePrice;ounceWeight = Double.parseDouble(df.format(ounceWeight));
            
            double weightList[] = {gramWeight, eighthWeight, quarterWeight, halfWeight, ounceWeight};

            
        }
    }
}

CodePudding user response:

Note quite sure I got what you're trying to do. If you want the highest value in your weightList array, simply do the following.

With Standard Java 8

final double maxWeight = Arrays.stream(weightList).max().getAsDouble();

With Other Libraries

Apache Commons Lang3's NumberUtils

final double maxWeight = NumberUtils.max(weightList);
  • Related