How do i get highest price index?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How do i get highest price index?



I have an issue about how do I get highest price Number in main class with class object ??



In getNumber function I only want to pass price and return number and in main i am creating 4 object with number which is auto increment and price so I want the highest price in which index it's located.i want to know that which position highest price is.


getNumber


Product.java import java.util.*;
import java.lang.*;
import java.io.*;

class Product

static int counter = 0;

int num;

double price;

TestCode()
Counter++;
num = Counter;


int getNumber(double price)
// what should i write here to get the number from price?
return num;




}



Main.java


import java.util.*;
import java.lang.*;
import java.io.*;

/* Todo;: get number which has highest price */

Class Main {
Public static void main(String args) {
int highestPrice = 0;
Product prod;
for (int i = 0; i < 4; i)

prod = new Product();
// What should I write here
If(highestPrice > prod.getPrice())
highestPrice = prod.getPrice()


System.out.println("highest price index at "+ prod.getNumber(highestPrice));






I'm sorry but I don't understand your question. Return the highest price of what? Of a single object? What does num represent? What do you want to return in getNumber?
– GBlodgett
Aug 8 at 18:14


num


getNumber





Also, your code is far from being valid Java code. Fix all the errors printed by the compiler.
– JB Nizet
Aug 8 at 18:15





In getNumber function i only want to pass price and return number and in main i am creating 4 object with number which is auto increment and price so I want the highest price in which index it's located.i want to know that which position highest price is.
– Vimal
Aug 8 at 18:17





why do you use two points instead of a semicolon in some of yours lines of code (i really don t know )?
– mrpepo877
Aug 8 at 18:20






@GBlodgett num is for index i am using static index which is common for all object so that I store the counter to maintain the index
– Vimal
Aug 8 at 18:24




1 Answer
1



So you have a lot of small errors here. (Remember case matters in Java)



public is lower case


public


public static void main(String args) {



You haven't initialized this variable:


Product prod;



Infinite loop here (You never increment i):


i


for (int i = 0; i < 4; i) {



Here you keep reinitializing prod:


prod


prod = new Product();



if is lower case:


if


If(highestPrice > prod.getPrice())
highestPrice = prod.getPrice()

System.out.println("highest price index at "+ prod.getNumber(highestPrice));



And in your Product class:


Product



This method doesn't have a return type. And you have counter as upper case here. (I'm assuming you wanted it to be the same lower case variable earlier in your code


counter


TestCode()
Counter++;
num = Counter;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard