• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quick question, average program problem.  RSS feed

 
Eric Matthew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason, the averages aren't calculating correctly. I'm aware I need to convert to double if I'm dividing by an odd amount but that itself isn't the issue. It's just simple easy divisible numbers that are not being calculated correctly. For instance, (10 + 10 + 10 + 50) / 4 = should be 2 but I get 10.

Here is the code:



Any guidance is appreciated. Thank you coderanch!
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, actually, the average should be 20, not 2, but I notice a wierd construct:


That would appear to be initializing a 1-element array and setting its first (only) value to the first (and only) value retrieved from the scanner. Which would be 10, for your given example. So 10/1 = 10.

I think you need to rearrange your loop a little.
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Matthew,

What you probably want to do, is to create an array of size x:
Notice where the "[]" placed, right after the data type, this is what convention says, not other way round as you did (after reference variable).
Then you could loop through an array size and populate with inputs (probably you want to have separate method for that, as well as for computing average). The rest pretty the same as you did.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That code won't compile. You probably meant:

The names 'x' and 'userInput' are not very good. Try to use names that explain their intent or purpose. Try 'count' or 'size' instead of 'x'. Try 'nextNumber' or something like that instead of 'userInput'.
 
Liutauras Vilda
Sheriff
Posts: 4916
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu is right about
As well as for variables naming ;)
Thanks for noticing.
 
Eric Matthew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys, really do appreciate it. I haven't exactly figured out the problem but I believe it stems from a not knowing a more intermediate topic regarding java, "ArrayList", that I'm not familiar with. ArrayList objects are dynamic which is what I was looking for as opposed to a static number of elements.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an array. The problem is that you're only getting input from the user once, on line 6. To get input multiple times, you need to put that line inside a loop, like a for-loop or while-loop. An alternative would be to just enter the values on the command line. The values will go into the args[] array. When you get your program working with that, then you can switch to getting input from the user in a loop. For example, if you run the program from the command line with this:

$ java Avg 1 13 15 8 2

The args[] array will have 5 elements in it. You will need to convert each value from String to int before you can do any arithmetic operations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!