• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to Compute and display the number of male and female respondents?  RSS feed

 
jojo chin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is:
Collect information of Name, Age, Gender, Income and the number of respondent is at most 1000.
a. Store data entered by the user
b. Display the data collected
c. Compute and display the average income
d. Compute and display the number of male and female respondents
e. Compute and display the average age
f. Sort the list based on increasing income


how to compute the number of male and female respondents? from the code i given below, it only counted all the respondents as female although i typed some male gender.


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When you post source code, please use code tags, so that the forum can show your source code in a better way. Also, please format the code properly - use indentation etc. so that the structure of the code is clear. Without proper formatting, code can be really hard to read.

Can you explain exactly what your question is?
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is probably your problem. Don't use == on String or in fact, any objects. Use it only for numbers. For Strings, use equals().
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jojo chin wrote:. . . i typed some male gender. . . .
It would probably be better to create an enumerated type MALE, FEMALE. You can actually use == with enumerated types. It even says so in the Java® Language Specification (=JLS). The JLS can be difficult to read, but I think that is one of its less difficult parts.

If you use Strings, don't use equals but equalsIgnoreCase(); you can pass male, Male, MALE or mALE and it will pick all those up.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!