• 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

Number counting Problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[javadoc]I'm new to this site and was hoping to find some help as I'm beginning to learn Java

the problem is: Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (excluding 0s). Your program ends with the input 0. Display the avg as floating number.

An example run: Enter an int, input ends if it is 0: 1 2 -1 0
Number of positives is 2
Number of negatives is 1
Total is 2
Average is 2/3

What I have so far is:




Whenever I input numbers, it doesn't terminate. And also, I'm not sure how to calculate the average if there is not a definite amount of numbers that will be entered. Any help is much appreciated, thanks
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typically your loop would read one double from the input. In your case, every iteration through the loop reads 4 or 5.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch

I would suggest you cannot sort that out on screen. Get a sheet of paper and a pencil and write down how you would work out such averages. Then you can convert it to code easily.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason your program is not terminating is that the method nextDouble and its relatives return a value, but you're not storing the value with a variable.
Another reason might be that your using nextDouble method instead of the hasNextDouble for checking the input stream.


Also, I would declare the pos and neg variables as integers as that is what you're supposed to be counting.

To get a definite number you just use a counter, which is a variable specifically used for counting.




Your code should similar to the one below
[solution removed]


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bat Boy wrote:Your code should similar to the one below...


Bat Boy,

First: Welcome to JavaRanch.

Second: Please don't post complete solutions; especially in the "Beginning Java" area. If you read at the top of the forum, it says:
"We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."
You'll notice that I've removed yours.

I'm sure your intentions were great, but we prefer to help people to find their own way, rather than just spoon-feeding them.

I suggest you read the HowToAnswerQuestionsOnJavaRanch (←click) page. It explains all about the ethos of this site.

Thanks.

Winston
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, i'll keep this in mind in future posts. Thank you.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic