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.

Results 1 to 12 of 12

Thread: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    jhhgjhhg
    Last edited by redbull; May 9th, 2015 at 04:19 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,205
    Thanks
    63
    Thanked 2,579 Times in 2,549 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    problem that I am having is that it is only collecting the first word of the input.
    If you are only entering one word before pressing Enter, use the nextLine() method to read the input from the user.

    For a test with the current code, try typing both words on the same line before pressing Enter. next() should read one and then the other.

    Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

    For example if you enter: A word to the wise <PRESS ENTER>
    and use next() only "A" is read, and "word to the wise" is left in the buffer.
    Your next attempt to get something from Scanner will be to get "word".

    To clear the buffer, use the nextLine() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,205
    Thanks
    63
    Thanked 2,579 Times in 2,549 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    nextLine() completely skips my input and essentially closes my program
    Please explain how that happens. Do you get any error messages?
    Can you post the code where that happens and copy the console from when you executed the code.
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    nextLine() reads a whole line.
    next() reads the next token.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    m
    Last edited by redbull; May 9th, 2015 at 04:18 PM. Reason: kjl

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,205
    Thanks
    63
    Thanked 2,579 Times in 2,549 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    You need to enhance your usage of println() to show what is in the String that was read. What if the String returned by nextLine() was empty? Try this:
    		System.out.println("g="+g+"<");

    Your problem is the buffering that Scanner does. next() does not remove the newline character from the buffer. When nextLine() is called it returns an empty line if the buffer has a newline.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    jhkjhj
    Last edited by redbull; May 9th, 2015 at 04:18 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,205
    Thanks
    63
    Thanked 2,579 Times in 2,549 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    Looks like an empty String was read into the g variable(between the = and the <). What method was used to read g?

    If the code has called the next() method, there is a newline left in Scanner's buffer.
    Call nextLine() to read that from the buffer before using nextLine() to read a whole line.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    kjklk
    Last edited by redbull; May 9th, 2015 at 04:18 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    24,205
    Thanks
    63
    Thanked 2,579 Times in 2,549 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    You need to have your PC's keyboard checked. It's Caps lock is stuck on.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    lhlkjk
    Last edited by redbull; May 9th, 2015 at 04:19 PM.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    lkjlkj
    Last edited by redbull; May 9th, 2015 at 04:19 PM.

  12. #12
    Member
    Join Date
    Sep 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME WITH THIS CODE. I"M ABOUT TO SMASH MY LAPTOP SCREEN

    ,hlkjkj
    Last edited by redbull; May 9th, 2015 at 04:19 PM.

Similar Threads

  1. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  2. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  3. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  4. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM
  5. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 03:21 AM

Tags for this Thread