• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing count  RSS feed

 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem and hope you can help. In my Modify_member method, im getting it to recognize count and print out as many names as I want.

In my report method, I'm starting small and JUST going with first name (fname). I have it set up what seems to be the similar way but im getting "null" as a response. ANy ideas???

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fname and count do not appear to be fields of your class, but rather only local variables of the main method. They appear to me to be out of scope of your report method. This is just on quick glance of your code, so I may be wrong.

Ah, I am wrong. You pass the variables as parameters. Still, you may want to make them static variables of the class. Nevertheless,
fname[count] == null because fname only has string values from index 0 to (count-1). If you do fname[count-1], you will get the first name of the last person entered in your array.
[ March 28, 2007: Message edited by: pete stein ]
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats the thing I dont get. I increment count in the While of the report method. I "think", what should be happening is that count gets initialized from whatever I do in the modify member method. SO........i put in three names...its initialized to three and when i call on report shouldnt it print the first 3 names??
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An even more important issue I have w/ the code is this: why you are storing all of your membership information separately in a bunch of discrete arrays? Wouldn't you rather have a member object that encapsulates all the info together with requisite methods, and then have a single array of this membership object?
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its part of the requirment. The next phase is going in and changing that information. Changing individual names...membership fees. Im told to have 7 parallel arrays.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by RJ Cavender:
Thats the thing I dont get. I increment count in the While of the report method. I "think", what should be happening is that count gets initialized from whatever I do in the modify member method. SO........i put in three names...its initialized to three and when i call on report shouldnt it print the first 3 names??


report will do what you have coded it to do, no more and no less.

As written "System.out.println(fname[count]);", it will try to output the "count" item in array fname, which in your code will always be null. If you enter 3 members, then count == 3 and the fname array will have 3 strings at locations:
fname[0]
fname[1]
fname[2]

if one tries to get fname[count] which is the same as fname[3] as you are doing, it will always return null.

To get what you want, you need a for loop going from i = 0 to i < count, and within the body of the loop, display fname[i].


Good luck!
[ March 29, 2007: Message edited by: pete stein ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you might want to consider changing menu() to return an int rather than a double and then casting this to int. Also, rather than using fixed arrays, are you allowed to use generic arraylists? They are much more flexible and type-safe than what you are currently using.
[ March 29, 2007: Message edited by: pete stein ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious, have you got it working yet?
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything but the very end. It figures. Ive been combing my textbook. I have to have an option where you indicate you want to list all the male ymca members or the female. I have M or F as strings. When I go to do a comparison like , meaning does what the user input match the gender for THAT person, i get an error. How can you do a selection statement on a string (namely M or F)?

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by RJ Cavender:
Everything but the very end. It figures. Ive been combing my textbook. I have to have an option where you indicate you want to list all the male ymca members or the female. I have M or F as strings. When I go to do a comparison like , meaning does what the user input match the gender for THAT person, i get an error. How can you do a selection statement on a string (namely M or F)?


String comparison requires that you call the equals method. The "==" operator will not work since it tries to compare the hash value of the string (I think), not the string values themself.

[** correction **]
this is wrong. I think that "==" doesn't work because it checks to see if the variables on both sides are the same object. The hash value for a string depends on the text of the string.
[/** correction **]

Something like:

or perhaps even better:


Might be what you are looking for.

For more basic info on comparing strings check out "Comparing Strings with ==".
/Pete
[ March 31, 2007: Message edited by: pete stein ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by RJ Cavender:
Its part of the requirment. The next phase is going in and changing that information. Changing individual names...membership fees. Im told to have 7 parallel arrays.

That's interesting, because I've really been questioning that approach. I didn't know whether to suggest something totally different or not.

 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll never beleive it. A dinky little $10 Java book i got from Barnes and Noble had it over those big New York Yellow pages Java books i have. I used

WORKED!!!
Thanks for your help though.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by RJ Cavender:
...
if (gender[ i ].startsWith("F"))
WORKED!!!...

That's cool, and probably just fine for this project. But for future development, some things to consider might be:
  • What if i is out of bounds?
  • What if gender[ i ] is null?
  • What if a lowercase 'f' is used?'
  • What if the user entered a space (or period, dash, etc.) before the 'F'?
  • What if the user entered some character other than 'F' or 'M'?
  • (Trust me. You wouldn't believe what real-world users enter. :roll: This is where input controls like radio buttons come in handy.)
    [ March 30, 2007: Message edited by: marc weber ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!