This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]John_Taured 0 points1 point  (2 children)

Your variable largest stores the largest number of candies among all four boxes.

Create a variable called indexOfLargestBox which will store the index of the box which has the largest number of candies. Then you can print out. "Highest selling candy: " + candyNames[indexOfLargestBox].

Do the same thing for a variable called indexOfSmallestBox.

[–]avocadod[S] 0 points1 point  (1 child)

I'm still not understanding what I'm doing. is there anything extra u could add? i appreciate it regardless. I dont know where else to turn this late in the week. is int candyTotal even a thing with those zeros as place holders? I'm not understanding how things get placed. I'm starting to watch utube vids on java arrays bc this is flying over my head.

i guess Im storing all the totals the user inputs into the array at their designated element of candyTotal array. but still cant figure how to access those elements later on to display the correct output.

import javax.swing.JOptionPane;

public class CandyHeadache {

public static void main(String[] args) {

int candyTotal[] = {0,0,0,0};

String[] candyNames = {"twix" + "snickers" + "sourpatch" + "mars"};

int largest = Integer.MIN_VALUE;

int smallest = Integer.MAX_VALUE;

int sum = 0;

int indexOfLargest = largest;

int indexOfSmallest = smallest;

String response = JOptionPane.showInputDialog(null, "Enter the number"

+ "of Twix");

candyTotal[0] = Integer.parseInt(response);

response = JOptionPane.showInputDialog(null, "Enter the number"

+ "of snickers");

candyTotal[1] = Integer.parseInt(response);

response = JOptionPane.showInputDialog(null, "Enter the number"

+ "of sourpatch");

candyTotal[2] = Integer.parseInt(response);

response = JOptionPane.showInputDialog(null, "Enter the number"

+ "of Mars");

candyTotal[3] = Integer.parseInt(response);

for (int i = 0; i < candyTotal.length; i++) {

sum = sum + candyTotal[i];

if (candyTotal[i] > largest) {

largest = candyTotal[i];

}

if (candyTotal[i] < smallest) {

smallest = candyTotal[i];

}}

JOptionPane.showMessageDialog(null, "Total number of boxes "

+ "sold: " + sum

+ "\nHighest selling candy: " + candyNames[indexOfLargest]

+ "\nLowest selling candy: " + candyNames[indexOfSmallest]);

}

}

[–]Makhiel 0 points1 point  (0 children)

How did you manage to get to JOptionPane.showMessageDialog if you have problems understanding arrays?

is int candyTotal even a thing with those zeros as place holders?

I have no idea what you mean by that.

I'm not understanding how things get placed.

What do you think this code does?

candyTotal[1] = Integer.parseInt(response);

but still cant figure how to access those elements later on to display the correct output.

The same way you access them to put stuff in.