Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

How do you add numbers in an user provided string?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on an assignment where I have to use methods to get a list of numbers as a string type and then another method to calculate the average of those numbers provided. I have been able to write the method that asks for the numbers but I'm stuck as for how to get the average of those numbers. I know average would be sum / by the number of numbers provided.

hers is the code I have so far. Can anyone provide some feedback or how I should go about this? I'm new to Java but it is tough at times but don't want to give up.



 
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First point is what format does the user enter their numbers?
And are you supposed to check that the format is correct?
 
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what you mean about adding numbers? Do you mean you want to add multiple numbers, or do you want to calculate the trasverse sum of a single number's digits?
Don't use nextLine(); use this method instead, in which case your method will retun an int.

Please look here to see how the code button is supposed to work; I have corrected the code tags for you because you are new, and doesn't it look better now Don't use so many empty lines in your code.
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what your code would look like with what I think is the right number of empty lines:-Don't mix indentation conventions: looks at lines 4 and 9.
 
Dave Tolls
Rancher
Posts: 3989
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't use nextLine(); use this method instead, in which case your method will retun an int.



Depends how you interpret this from the OP:
" I have to use methods to get a list of numbers as a string type"

To me (along with the code as written) the numbers come in as a single String, hence my question about the format of the input.
 
Ranch Hand
Posts: 49
1
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understood right, you want to get some numbers from the user as a string and then to find their average.

To do it you can create a String array or list ( ArrayList<String> ) to collect numbers which are the user entered. And then write another method which takes this array or list as a parameter and returns double.Then iterate this array/list and take each element as Double.parseDouble(yourStringElement), add them to some variable( for example sum) and finally devide this sum to the length of array/list. Then return what you get.

HIH
 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like the requirements are open to interpretation. All keyboard input comes in as characters.
  • You could get them as a list of numbers entered on a single line using Scanner#nextLine(), but then you'd have to separate then numbers and take each number (String) and parse it into an int (using Integer#parseInt()) for processing.
  • You could use Scanner#next() to split the numbers into individual Strings but then you'd still have to parse them into ints.
  • You could use Scanner#nextInt() which both splits the numbers and parses them into ints.


  • The last approach would be my preference but it depends on how you interpret the specs. With the last approach you end up with a list of ints that you can sum and count for your average but you'd have to print them to the display, which is trivial.
     
    Campbell Ritchie
    Marshal
    Posts: 63843
    209
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would prefer Scanner#nextInt() myself; of course you can combine that with Scanner#hasNextInt() in a loop to read ints until you enter something not an int.
     
    Won't you be my neighbor? - Fred Rogers. tiny ad:
    global solutions you can do in your home or backyard
    https://coderanch.com/t/708587/global-solutions-home-backyard
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!