• 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

I need help with a program in java.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to write a program that accepts user input and then prints its average in a serial way.

Suppose i enter 5. I should get result 5 first time then i input 10 then it should return the average of (5 and 10) and then i enter say 20 it should return average of (5,10 and 20) and so on.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok...so what have you tried? Where exactly are you stuck?
 
altamash khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have tried to use a loop but the main part where i am stuck is at how to serially average the user input....
i can use arraylists to store user input ... can I?
 
altamash khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i wish you could help me out
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

altamash khan wrote:i have tried to use a loop but the main part where i am stuck is at how to serially average the user input....
i can use arraylists to store user input ... can I?



Well, which part are you stuck with? How to average the user input? Or how to store the user input? IMO, storing it should be tackled first.

Also, what problems have you ran into using the loop?

Henry
 
altamash khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i have difficulty in combining everything. I think storing could be done using arrayLists within a loop and averaging could be done with a method that dynamically sees the array and averages the elements and prints it out . Problem is how to combine everything. awaiting your reply
 
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
It's best if you show us (some of) the code you have written so far, in particular, the parts where you are having problems. When posting code, please UseCodeTags (←click on that link to learn how)
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would also suggest you StopCoding. Come up with the algorithm first. If all you had was paper, pencil, and and eraser, how would you keep track?

Think about how you find an average. Do you really, REALLY need each and every value provided so far (hint - you don't, if you have two other single pieces of information)?

It doesn't do any good to program until you are SURE how you want to do it. Can you store every value? yes. That is a perfectly acceptable way to do it. But when you say "i think it could be done like this...", that makes me believe you haven't thought through the problem enough.
 
altamash khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class apples2 {
public static int average,temp,total= 0;
public static void main(String[] args) {

Scanner altamash = new Scanner(System.in);

ArrayList<Integer> arrayList = new ArrayList<Integer>();

System.out.println("Enter numbers");

while(altamash.hasNextInt()){
temp = altamash.nextInt();


arrayList.add(temp);

for(int x : arrayList){
total += x;
}
average = total/arrayList.size();

System.out.println(average);
}
}
}
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

altamash khan wrote:



First, you were asked to use code tags to make it more readable. Please follow instructions.
On line 17 you loop through the array without setting total to zero. On line 2 you set it to zero but on 18 you add total to inself each time through the while() loop. Or you could just eliminate the for() on line 17 but keep total += temp on line 18.
 
altamash khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot . Problem solved . There was no need to use the for loop, but just in case if i used the for loop then what should i do to get the same output.
 
altamash khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in fact .. i don't even need an arraylist , for averaging i can use "int count" and then "average = total/count " ;
and sorry for not following instructions; i am new here and this was my first post in this forum.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a way you can do that with a Stream. But I can't remember how. You get a stream from the list, map it to an int then you get an IntStream, and call its average() method.
 
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic