• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Quick question, average program problem.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic