# Arrays/Looping probelm?

• February 6th, 2012, 01:58 PM
dx8292
Arrays/Looping probelm?
PROBLEM SOLVED....THANK YOU. I thought there was too much code just in case someone wanted to copy the entire thing, so I deleted it.
• February 6th, 2012, 02:18 PM
Tjstretch
Re: Arrays/Looping probelm?
Alright, first of for your end number, 111, would it be a valid number to get the average of? Would it be unreasonable for someone to use the number 111 when calculating an average? Can you think of any number that would, if in the list of numbers, be skipped? [Hint: This number would not affect the sum at all.]

Also, because you are not sure the size of the array, this would be a great time for an ArrayList, which will do all the work for you [Although it speeds up the process if you give it a rough estimation about how many numbers, EG 15]. However if you cannot, for some reason, use an arraylist, could you think of any way to send how many numbers were actually used? (Hint, send)
• February 6th, 2012, 02:53 PM
dx8292
Re: Arrays/Looping probelm?
I'm not too familiar with ArrayList.

Can you use ArrayList with the Scanner and Array?

array.size() or numinput.size() //like I said, not too familiar with it
• February 6th, 2012, 03:02 PM
Tjstretch
Re: Arrays/Looping probelm?
You could use an ArrayList with a scanner, but I'm not sure what you mean by with an Array?

Example of using a scanner
Code Java:

```/* * Precondition: END_VAR is a int that is initialized */ Scanner in = new Scanner(System.in); int i; ArrayList<Integer> array = new ArrayList<Integer>(); System.out.print("Type your first integer"); i = in.readInt(); while(i != END_VAR) { array.add(i); System.out.print("Type another integer or type "+END_VAR+" to exit."); i = in.readInt(); } System.out.println("You listed these numbers: "+array);```

Example run (If it was called):
Code :

```Type your first integer 5 Type another integer or 111 to exit. 26 Type another integer or 111 to exit. 45 Type another integer or 111 to exit. 15 Type another integer or 111 to exit. 111 You listed these numbers: [5, 26, 45, 15]```

As for its size, as you said you would use .size()... instead of .length in a regular array.

http://docs.oracle.com/javase/7/docs...ArrayList.html
• February 6th, 2012, 03:39 PM
dx8292
Re: Arrays/Looping probelm?
This does seem less complicated, but I think am supposed to stick with arrays. From what I have looked up ArrayList "is similar to Arrays," which means I can't use it with an array, unless I'm interpreting it the wrong way.

Do you think it would work if I did array.size() or would it be numinput.size();?
• February 6th, 2012, 03:49 PM
Tjstretch
Re: Arrays/Looping probelm?
Ok the size() method is on objects only, if you were using a regular array you would nee to use the variable length, like:
Code Java:

```/* * PRECONDITION: array is a regular array that is not null */ System.out.println("The array has "+array.length+" things in it.");```

However, would it make sense to pass that every time, if that method already has access to the size of the array?

Hint: The counter variable you increment every time the user enters a number might be useful to your average method (The number of useful numbers in the array) Although I just
realized that since the array is going to initialize the numbers to 0, this will only make it more efficient, not necessarily more accurate.
• February 6th, 2012, 05:21 PM
dx8292
Re: Arrays/Looping probelm?
FINALLY GOT IT.....took me half the day for something so ridiculously easy, but I GOT IT.

Just took out the average method and put the code directly into the main method and did total/count.

TJstretch thanks for helping with the ArrayList and everything....I'll use that next time when I'm not limited to just Arrays.
• February 27th, 2012, 07:44 AM
JavaPF
Re: Arrays/Looping probelm?
Do not delete your original thread! This makes the thread useles for people with the same issues.