• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Calculating average of a string of numbers  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Im trying to write a code that uses 3 methods that follows specific guide lines. These are the guide lines:
The program uses methods to:

Get the numbers entered by the user
Calculate the average of the numbers entered by the user
Print the results with the whole number, a decimal, and two decimal positions
The first method should take no arguments and return a String of numbers separated by spaces.

The second method should take a String as its only argument and return a double (the average).

The third method should take a String and a double as arguments but have no return value.

For example, if the user input is...  20 40 60 80 100

...the program should give as output...  The average of the numbers 20 40 60 80 100 is 60.00

The first method does get a line of numbers form the user but I'm stumped as to how to writew a method that calualtes the average for thos numbers. I know the second method would have to have a sum oif the numbers entered and then that sum be divided by the numer of numbers entered. I dont how sum the numbers entered since thewy are in a string and dont know how to find the average, I hough of using .length() as a way toi find that average but that didnt work. Any iodeas?
 
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Strings typically can't be added as far as value input. You were using strings. Best thing to do is use a while loop and use a 3rd int as a counting mechanism for it. If you want to use decimals change "int" to "double"
 
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an error‑prone way to enter numbers; you are going to get the first line of text entered, whether it contains numbers or not, and you mightn't be able to use that text to calculate anything.
Why have you made so many things static? I look on it as a mistake to write static without a good reason.
Maybe you can create a StringBuilder and use its methods to create a String containing all your ints. Remember you can write code with multiple method calls, like this:-If you go through the methods of Scanner, you will find methods telling you whether the next token constitutes an int, so you can write a loop if you wish.

It seems strange to make up a String and then take it apart again to make individual numbers. You can analyse your String with a Scanner, if you wish:-
 
Campbell Ritchie
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LA: welcome to the Ranch

I am afraid your solution only repeats the error made earlier of making variables static. You are also not counting numbers entered; you are using five numbers regardless.
 
Greenhorn
Posts: 14
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do this to ensure fool-proof.  
Print "Please input numbers, and input "calculate" to calculate the average."
1. Get an input scanner.
2. Initialize a boolean "breakRequested" and "notDigit" as false
3. Initialize arrayList of ints
4. While breakRequested is false, if input.equals("calculate") is not true
5. for(char c :input.toCharArray()){
6. if c.isDigit()
7. Nothing
8. Else notDigit = true and break;
9. if notdigit is false
10. add input.parseInt to the arraylist
11. Else print "please input only numbers."
and if the input equals calculate is true you set breakRequested to true and break. It ends the loop.
Probably not the best answer. I did not actually write the code to see if it is practical, but I hope
it helps.

 
Campbell Ritchie
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks a very complicated way to look for numbers, particularly when you have been provided with a reliable and well‑tried technique.
 
F Lucas
Greenhorn
Posts: 14
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That looks a very complicated way to look for numbers, particularly when you have been provided with a reliable and well‑tried technique.


I just wanted to give an answer that accepts any amount of inputs. OP did not specify how many numbers he wants to calculate.
 
Campbell Ritchie
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

F Lucas wrote:. . . OP did not specify how many numbers he wants to calculate.

But you can write a loop using a method of a Scanner that takes input as long as there are ints to count. The loop will stop if you enter a non‑int, like this, containing eleven numbers:-You will get the same eleven numbers from my loop if you write them on separate lines:-
 
Bartender
Posts: 700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want to be foolproof, you cannot just add up the numbers like that and then divide by n.
Whata happens if one of the numbers is near Integer.MAX_VALUE ? You run the risk of overflow, even though the true average is representable.
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!