Hello, I'm working on an assignment due next week and I'm having quite a bit of trouble.

The program is to ask the user for input of a double until they desire to stop. That part, I imagine will be in the Test file. The master file is to store the inputs in an array list, count how many instances of doubles there were, and then output each variable, add them all the together, find the average, the largest, and the smallest value. I know this is probably really easy, but I haven't been in Java for 2 years so I'm struggling and I need to know what I'm doing wrong if, that is I'm doing anything right at all lol.

Example of desired output:

Please enter a double value: 3.5
Another?: Y
Please enter a double value: 4.3
Another?: Y
Please enter a double value: 7.3
Another?: N

Value 1: 3.50000
Value 2: 4.30000
Value 3: 7.30000

Sum: 15.100000
Average: 5.033333
Largest: 7.300000
Smallest: 3.500000

List cleared.

---My code so far---

public class ArrayList
{
        private double sum, average, large, small;

        public ArrayList()
        {
                final int MAX_ARRAY = 15; //hold up to 15 values
                double[] list = new double[ MAX_ARRAY ];
        }

        public Data()
        {
                for(int count = 1; count < list.length; count++)
        }

        public void process(list)
        {
                double sum, average, large, small;

                for(int count = 1; count < list.length; count++)
                        sum += list[ count ];

                for(int count = 1; count < list.length; count++)
                        average = list[ count ] / count;

                for(int count = 0; count < list.lenght; count++)
                        if(large < list[ count ];)
                        {
                                large = list[ count ];
                                large = count;
                        }

                for(int count = 0; count < list.length; count++)
                        if(small > list[ count ];)
                        {
                                small = list[ count ];
                                small = count;
                        }
        }

        public display()
        {
                return list;
                return sum;
                return average;
                return large;
                return small;
        }

        public clearData()
        {
                list[] = 0;
                count = 0;
        }
}

Any help is appreciated, thanks.

Recommended Answers

All 9 Replies

Please use code tags when entering your code. Use the icon above to right.

what I'm doing wrong

If there are errors, please copy the full text and paste it here.

Is the program generating the correct output?
Your posted output has no comments showing if or where there is a problem.

Can you explain why you think something is wrong?

Here are some observations to begin with:

- You do know, don't you, that there is an ArrayList class available to you already?

It's a pretty handy class, you should look it up. (pick your favorite search engine and look for "java arraylist" and the complete description from Sun will likely be the first thing that you see)

So I'm not sure whether you're trying to use the existing ArrayList class or to define your own.

- I also see that you've got a constructor for a class Data that isn't in a class by that name - this will be giving the compiler some heartburn.
That constructor doesn't actually do anything, though, it just advances a counter through a list that it won't have access to and then disposes of the number it finds (which is in any case available to you by calling list.length, which you do)

- Very rarely will you need to start a loop at an explicit 1 - in some sort algorithms, perhaps, but generally you're going to start counting at 0 or at some point given by a variable - an index in an array or a string, for example. Since your loop there doesn't do anything, it doesn't matter, but remember than the next time you're tempted to write for (int i = 1;...) - it's probably not what you want.

- You're also getting random smiley faces because you haven't used the code tags - use the code tags please! - and that's making it a little hard to read.

If you can step back from the code and tell us how you're trying to do this, using English, not Java, it would be easier to help you, and you might benefit from the exercise as well.

Or you could wait around for some nimrod to write your homework for you, that might happen too...

Adding a few more observations:
- Those "wink" smilies are showing due to ;) , which is invalid syntax in those if() statements.

- No parameter type in the process() method declaration.

- Multiple return statements in the display() method - code beyond the first is unreachable. And the method declaration itself doesn't have a return type stated, so it's an invalid declaration.

Ok, what I'm trying to do with the getData() method (what I renamed it) is make sure the user inputs only up to 15 values (MAX_ARRAY), but I have no idea how to monitor that through the for loop.

Also, the count = 1 ordeal was only because I had a feeling that instead of outputting "Value 1, 2, 3" it would start from 0 which would be incorrect in this case if I used System.out.printf("%s%2d%s%.6d", "Value", count, ":", list[ count ]); for the output. I'll revise my code and post it shortly. Thanks for the quick answers!

Is there any reason you want to limit the user to giving only fifteen values? Is that part of the requirements, for example?
Or is it because your array has fifteen slots?

I would be remiss, I think, if I didn't point out that it's possible to do this without storing all of the data the user gives you...

public class cheese_ArrayList
{
        private double sum, average, large, small;
        private double[] list;

        public void cheese_ArrayList() //tell array to hold up to ARRAY_MAX values
        {
                final int MAX_ARRAY = 15; //store max number of allowed inputs
                double[] list = new double[ MAX_ARRAY ]; //store double values input by user
        }

        public void getData() //repetition structure to read up to ARRAY_MAX values
        {
                //number of inputs recorded
                //limit user to a max of 15 inputs
                //store actual number of input in count
                for(int count = 0; count < list.length; count++)

        }

        public double void processData() //calculate sum, average, large, small and store in list
        {
                double sum, average, large, small;

                for(int count = 0; count < list.length; count++)
                        sum += list[ count ];

                for(int count = 0; count < list.length; count++)
                        average = list[ count ] / count;

                for(int count = 0; count < list.lenght; count++)
                        if(large < list[ count ])
                        {
                                large = list[ count ];
                                large = count;
                        }

                for(int count = 0; count < list.length; count++)
                        if(small > list[ count ])
                        {
                                small = list[ count ];
                                small = count;
                        }
        }

        public static displayData() //repetition structure show values in list (array)

 {
                System.out.printf("%s%d%s%.6d", "Value ", count, "= ", list[ count ]);
                System.out.printf("\nSum of all values = %d", sum);
                System.out.printf("\nAverage of all values = %d", average);
                System.out.printf("\nLargest value = %d". large);
                System.out.printf("\nSmallest value = %d", small);
        }

        public clearData() //set all instance variable values back to zero
        {
                list[ count ] = 0;
                count = 0;

                System.out.println("The list is now cleared.");
        }
}

Here is a slight update. And yes, the 15 is a requirement. Apparently the default is a limit of 10. It might be an exercise just to make sure I can change the default for array slots.

private double[] list;
 
        public void cheese_ArrayList() //tell array to hold up to ARRAY_MAX values
        {
                final int MAX_ARRAY = 15; //store max number of allowed inputs
                double[] list = new double[ MAX_ARRAY ]; //store double values input by user

You have defined the variable 'list 'in two places and given one of them a value; the other will be null.
Look at your other variables also.

Apparently the default is a limit of 10.

Ah. I see now. Yes, you're meant to be using the built-in ArrayList class, not defining your own class called ArrayList. Have a look here and see what you can learn.

Especially look at the constructors provided for this class. You'll see that there are three ways to instantiate one, and one of those three allows you to explicitly set the initial capacity.

It's a silly thing to put in the assignment, though, because ArrayLists will automatically resize themselves if they need to.


Go ahead and read over that link, get familiar with the class and its methods, and then we'll figure out what it is you need to know from there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.