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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Last edited by dx8292; February 6th, 2012 at 06:23 PM.
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)
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
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
/* * 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):
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
Last edited by Tjstretch; February 6th, 2012 at 04:05 PM.
dx8292 (February 6th, 2012)
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();?
Ok the size() method is on objects only, if you were using a regular array you would nee to use the variable length, like:
/* * 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.
dx8292 (February 6th, 2012)
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.
Do not delete your original thread! This makes the thread useles for people with the same issues.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.