• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need help with a program in java.  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
i wish you could help me out
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!