Basically the program has to ask for user input of a sequence of numbers but I don't know how to prompt a sequence of numbers to be input. Also, the first number read should be the length of the sequence. That number isn't part of the sequence but it specifies how many numbers will be input by the user. . I'm stuck on how to get started with the user prompt and how I can apply the formula for mean for example n a sequence. Thank you :)
Asked
Active
Viewed 1,033 times
-11
-
2There must be at least one thousand examples of doing this exactly as you describe on the Internet already. I suggest you try google. BTW You can do this without creating an array or a saving the values. i.e. perform the calculation as you read it. – Peter Lawrey Jan 17 '16 at 21:02
-
@PeterLawrey thank you for the code, umm I want to do it by performing the calculation. The code you gave me gave me a syntax error at where the brackets are for .summaryStatistics. – Leonardo Jan 17 '16 at 21:18
-
The code ran for me. I assume you have Java 8 and you copy pasted the code and corrected the imports using your IDE? – Peter Lawrey Jan 17 '16 at 21:18
-
As Pshemo mentioned, http://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java is a good link on how to read numbers from input. – Peter Lawrey Jan 17 '16 at 21:19
-
@PeterLawrey yep, Java 8, I copied and corrected it as it came. Ooo, thank you, will look at that too. – Leonardo Jan 17 '16 at 21:23
-
1Sorry but I'm voting to close this question as off-topic because "[3. Questions asking for homework help must include a ***summary of the work you've done so far to solve the problem***, and a ***description of the difficulty you are having solving it***.](http://stackoverflow.com/help/on-topic)". We can't solve your homework since its purpose is not only to *have* solution, but to *learn how to create one*. Show us [what have you tried](http://mattgemmell.com/what-have-you-tried/) and what is stopping you to continue/finish writing your code. – Pshemo Jan 17 '16 at 21:26
1 Answers
2
While there is many examples of this for Java 1.0 - 7, I am adding an example for Java 8.
Scanner in = new Scanner(System.in);
DoubleSummaryStatistics stats = IntStream.range(0, in.nextInt())
.mapToDouble(i -> in.nextDouble())
.summaryStatistics();
System.out.println(stats);
for input
4
5
2
3
1
prints
DoubleSummaryStatistics{count=4, sum=11.000000, min=1.000000, average=2.750000, max=5.000000}

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
-
If you are going to use summary statistics of int stream then we already have questions and answers about it... – Pshemo Jan 17 '16 at 21:05
-
@Pshemo close, if there is a duplicate for this answer, I will happily close this question. – Peter Lawrey Jan 17 '16 at 21:07
-
1There are many subquestions here which can be closed with other questions like http://stackoverflow.com/questions/5287538/how-can-i-get-the-user-input-in-java or one which is using statistics (I picked one which I answered some time ago) http://stackoverflow.com/questions/31083405/how-to-get-min-max-value-from-list-of-objects-using-java-8/31083591#31083591 – Pshemo Jan 17 '16 at 21:09