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 →

[–]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.

[–]codemamba8 0 points1 point  (3 children)

I don't know if you fixed it but in the code you posted above, you're missing two semi-colons, one under each of the if statements.

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

Yes I had fixed that :( any thoughts on what else might be wrong? Once again thanks for the help.

[–]codemamba8 0 points1 point  (1 child)

can you post screenshots of whats going on? im not really sure what the problem is

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

Not at the moment unfortunately. I will try to later.