Trying to show my highest number of sales from my array show with the month they happened on a Message dialog on the JOptionPane but am failing to find a way to show the month
public static void main(String[] args) {
int[] CarSales= {1234,2343,1456,4567,8768,2346,9876,4987,7592,9658,7851,2538};
String [] Months = {"January","February","March","April","May","June"
,"July ","August","September","October","November","December" };
int HighNum = CarSales[0];
for(int i = 0; i < CarSales.length; i )
{
if(CarSales[i] > HighNum)
{
HighNum = CarSales[i];
}
}
JOptionPane.showMessageDialog(null,"The highest car sales value is :" HighNum
"-which happened in the month of");
}
}
CodePudding user response:
There are multiple solutions but i'll give you the simplest based on your structure. Just declare one String variable and assign the value whenever you change the highest num.
public static void main(String[] args) {
int[] CarSales= {1234,2343,1456,4567,8768,2346,9876,4987,7592,9658,7851,2538};
String [] Months = {"January","February","March","April","May","June"
,"July ","August","September","October","November","December" };
int HighNum = CarSales[0];
String month = Months[0];
for(int i = 0; i < CarSales.length; i )
{
if(CarSales[i] > HighNum)
{
HighNum = CarSales[i];
month = Months[i];
}
}
JOptionPane.showMessageDialog(null,"The highest car sales value is :" HighNum
"-which happened in the month of " month);
}
CodePudding user response:
Use the Power of Objects
Avoid using parallel arrays in Java. It brings unnecessary complexity, makes the code brittle and less maintainable.
Your code doesn't automatically become object-oriented just because of the fact that you're using an object-oriented language.
Objects provide you an easy way of structuring your data and organizing the code (if you need to implement some functionality related, to a particular data you know where it should go - its plane is in the class representing the data).
So, to begin with I advise you to implement a class, let's call it CarSale
:
public static class CarSale {
private Month month;
private int amount;
// getters, constructor, etc
}
Or if you don't need it to be mutable, it can be implemented as a Java 16 record
.
public record CarSale(Month month, int amount) {}
java.time.Month
You've probably noticed that in the code above, property month
is not a String
. It's a standard enum Month
that resides in java.time
package.
When you have a property that might have a limited set of values, enum
is always preferred choice because it contrary to a plain String guards you from making typo and also enums have an extencive language support. So you don't need this array filled with moth-names.
That's how your code might look like:
CarSale[] carSales = {
new CarSale(Month.JANUARY, 1234),
new CarSale(Month.FEBRUARY, 2343),
new CarSale(Month.MARCH, 1456),
// ...
};
// you might want to check if carSales is not is empty before accessing its first element
CarSale best = carSales[0];
for (CarSale sale: carSales) {
if (sale.getAmount() > best.getAmount()) best = sale;
}
JOptionPane.showMessageDialog(null,
"The highest car sales value is :" best.getAmount()
" which happened in the month of " best.getMonth());
CodePudding user response:
Keep an index. Whenever you change the highest found, update the index.
public static void main(String[] args) {
int[] CarSales= {1234,2343,1456,4567,8768,2346,
9876,4987,7592,9658,7851,2538};
String [] Months = {"January","February","March","April","May","June"
,"July ","August","September","October","November","December" };
int HighNum = CarSales[0];
int highMonth = 0;
for(int i = 0; i < CarSales.length; i )
{
if(CarSales[i] > HighNum)
{
HighNum = CarSales[i];
highMonth = i;
}
}
JOptionPane.showMessageDialog
(null,"The highest car sales value is :" HighNum
"-which happened in the month of " Months[highMonth]);
}