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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: Java Help

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Question Java Help

    Deleated


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    Sorry about that, fixed.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    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.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    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()?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    Essentially, yes. If you've gotten the desired data, as in:

    double neededData = input.nextDouble();

    you can "flush the buffer" immediately afterwards with

    input.nextLine();

    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.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    solidMGSsnake (December 9th, 2013)

  8. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    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?

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    Are you using the comparison operator, '==', instead of the equals() method to compare Strings?

  10. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    Yes how do I do it the other way?

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    if ( thisString.equals( thatString ) )

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    solidMGSsnake (December 9th, 2013)

  13. #11
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    610
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Programming Assignment

    of the equals() method to compare Strings?
    It's right there

    EDIT: dammit, Greg.

  14. The Following User Says Thank You to PhHein For This Useful Post:

    solidMGSsnake (December 9th, 2013)

  15. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    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?

  16. #13
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    610
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Programming Assignment

    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.

  17. #14
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    Deleated

  18. #15
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    610
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Programming Assignment

    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.

  19. #16
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    Deleated

  20. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    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 )

    else ()

    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 )

    else ()

    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.

  21. #18
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    Deleated

  22. #19
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    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.

  23. #20
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    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.

  24. #21
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    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.

  25. #22
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    Deleated

  26. #23
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    Because of this:

    paid = scan.nextLine();
    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.

  27. #24
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Programming Assignment

    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?

  28. #25
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: Programming Assignment

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with programming assignment! New!
    By thejewishpriest in forum Member Introductions
    Replies: 1
    Last Post: April 15th, 2013, 03:30 AM
  2. Help on programming assignment
    By aaronmcohen in forum AWT / Java Swing
    Replies: 0
    Last Post: March 7th, 2012, 06:47 PM
  3. Programming help with java assignment!
    By kami21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2011, 06:37 PM
  4. Programming assignment using inheritance
    By Slypher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2011, 05:42 PM
  5. programming assignment help
    By sdgseed in forum Threads
    Replies: 1
    Last Post: October 20th, 2010, 02:43 PM