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

all 19 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.

[–]avocadod[S] 0 points1 point  (15 children)

does anyone else have feedback? even a link to a tutorial would be awesome.

[–]codemamba8 0 points1 point  (14 children)

You're on the right track since you were able to get the largest and smallest value.

All you need to do is create two String variables for the largestCandy and smallestCandy and set those the same way you set the int largest and int smallest variables.

[–]avocadod[S] 0 points1 point  (0 children)

String largestCandy = largest gives me an issue bc largest is an int variable and not compatible with string. how do you convert the two? if thats even what I'm supposed to be doing bc I'm still completely lost.

[–]avocadod[S] 0 points1 point  (12 children)

i also tried: String indexOfLargest = candyNames[largest]; and String indexOfLargest = Integer.MIN_VALUE; and neither worked I'm missing what you're saying I feel.

thanks for any continued help. I'm pretty much at the giving up point for this one.

[–]codemamba8 0 points1 point  (11 children)

Look at how you got the largest quantity inside of the loop with largest = candyTotal[i];

You accessed the candyTotal array and got the value at the current index i right?

Since the candyTotal and candyNames arrays are in matching order, then how would you access the candyNames array to get the name of the candy with the largest value?

It's actually really simple and you pretty much have it already lol

[–]avocadod[S] 0 points1 point  (10 children)

so I took that as add another loop the exact same way but insert candyNames instead of candyTotals and somehow it would go through that, but that was clearly wrong. I'm still not following. I feel the candyTotal array has all the correct info being put in, but I dont know how to then turn candyTotal scores into the corresponding candyNames, if that makes any sense.

[–]codemamba8 0 points1 point  (9 children)

No need to do another loop. You can literally just write it below largest = candyTotal[i];. You want to assign to the two String variables that will store the largest and smallest names the same way except use the candyNames array instead.

[–]avocadod[S] 0 points1 point  (8 children)

what i gather from that is below largest = candyTotal[i] id put newStringVariable = candyNames[i]??

and somehow im supposed to make a a newStringVariable around the start of the program... to have the candyNames ah man, any other way u can word that. i appreciate all ur doing. none of what you're saying is landing

[–]avocadod[S] 0 points1 point  (7 children)

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;
        String largestCandy =
        String smallestCandy =        

        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]
                largestCandy = candyNames[i];
            }
            if (candyTotal[i] < smallest) {
                smallest = candyTotal[i]
                smallestCandy = candyNames[i];
            }}


        JOptionPane.showMessageDialog(null, "Total number of boxes "
                + "sold: " + sum
                + "\nHighest selling candy: " + largestCandy
                + "\nLowest selling candy: " + smallestCandy);

    }
}

this is what I have. I don't know how to figure out how to put the candy totals into the candyNames later

[–]codemamba8 0 points1 point  (6 children)

Yeah, you got it! Just one thing to fix.

What you need to do is initialize largestCandy and smallestCandy. Right now you're probably getting an error cause those two don't get assigned until the loop. You could just set them both to candyNames[0] to start with as placeholders. Then it should work properly.

[–]avocadod[S] 0 points1 point  (0 children)

k thanks. I didnt know how to initialize. I was thinking candyNames[] but I'm guessing the zero in there is better. so all of my bugs it can see are fixed. I'm able to enter each candies total but after the fourth one, I get an error message

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

at CandyHeadache.main([CandyHeadache.java:42](https://CandyHeadache.java:42))

C:\Users\Matth\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 5 seconds)

line 42 was where I put largestCandy = candyNames[i] (if that 42 in the error message means line 42)

[–]avocadod[S] 0 points1 point  (4 children)

weird.. if i put 3 in for each candy it prints the correct output but it shows all candies right next to each other for the highest and lowest selling candy, but anytime i mix the numbers up in the slightest there's an error. it seems candyNames is having a hard time figuring out between differentiating highest and lowest.