Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
Members have full access to the forums. Advertisements are removed for registered users.
Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.
Sorry about that, fixed.
Scanning for a specific token, usually a numeric value like int, double, etc., does not capture and clear the linefeed character in the input buffer. If that Scanner statement is followed by one that reads an entire line in the buffer, then the remaining linefeed IS read and accepted as the input, causing that next input to appear to be skipped. To avoid this behavior, simply "flush the buffer" after the numeric input has been accepted with a Scanner.nextLine() statement that gets thrown away.
I'm sorry I'm a bit of a noob, could you explain what to do in rudimentary terms? Are you saying to but a Scanner.nextLine() after the Scanner.nextDouble()?
Essentially, yes. If you've gotten the desired data, as in:
double neededData = input.nextDouble();
you can "flush the buffer" immediately afterwards with
If there are several nexts that gather numeric input in a row, the flushing only has to occur after the last one, and will probably cause problems if you add more than that.
Well that worked to get me past "enter membership fee" and allowed me to answer the question "Has the fees been paid?" But if I enter "yes" or "no" then it still says invalid option despite the if statement.
--- Update ---
Sorry for the bump but could you reply as fast as possible as this is due tomorrow and I really need to finish it?
Are you using the comparison operator, '==', instead of the equals() method to compare Strings?
Yes how do I do it the other way?
if ( thisString.equals( thatString ) )
It's right thereof the equals() method to compare Strings?
EDIT: dammit, Greg.
Ya that wouldn't work but I found out that the yes or no had to have quotation marks enclosed as well. Ok guys that works now, if I need more help with this program, is it ok if I post in the same thread?
Yep, that's fine. But please post the updated code when asking another question. Your code should have changed quite a bit since you posted the original question.
OK, that looks nasty, but just look at the first error and resolve that. You have a typo in the line of code that the first error message points to.
You need to think these things through a bit more and thoughtfully TRY things. You're getting frustrated and giving up too early.
Your current construction is:
if ( yes )
if ( no )
The else is paired with the last if ( no ) and doesn't care a bit about the first if ( yes ). You need:
if ( yes )
else if ( no )
As for not running, you've posted a few versions here. Lacking a better version control system, fall back to the last version you posted here that works and move forward from there.
Again, take a breath, read the error, look at the code it is pointing at, and take a moment to think.
A few thoughts should occur to you: '=' is the assignment operator, '==' is the comparison operator, and '==' should not be used to compare String objects. "2" is a String, 2 could be (and will be assumed by the compiler to be) an int.
Ya I actually found that myself after. Honestly I don't have time to take a breath because this is due tomorrow and I still need to do a lot . Also I'm still new to Java so it takes me ages to find errors to begin with, especially if the program will compile anyway. So I apologize if I'm being reckless but time is of the essence.
If I do a full cycle of my choice(1) after I answer "yes" or "no" to the fees question. I have to press enter again before it prints the thank you message.
If you're asking for help by this statement, please post your latest code, and, as appropriate, a sample run that shows problems/undesirable behavior, current issues, specific questions, and any error messages you're receiving.If I do a full cycle of my choice(1) after I answer "yes" or "no" to the fees question. I have to press enter again before it prints the thank you message.
Because of this:
paid = scan.nextLine();
The nextLine() method will wait until the buffer contains a linefeed. You have two of them in a row, so two <returns> are required.
You told me to put that in to fix another error, I don't know how to put in <returns> and I'd say my lecturer would know I didn't do it myself if I did. Any other way around it?
You're being a bit dense. I told you that a nextLine() was required after collecting numeric input because nextInt(), nextDouble(), etc. leave a linefeed in the input buffer. In the case shown, numeric input is not being collected. The variable 'paid' is not numeric, nextInt() or nextDouble() is not being called.
The <returns> are keyboard input by the user, not something you add to your program.
Just delete the second scan.nextLine(), the one after 'paid' is assigned to the first.