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 9 of 9

Thread: Code is ignoring the read.

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Code is ignoring the read.

    As you can see there is a prompt at the very bottom of this code, The prompt is followed by a read. The code skips the read and ends instead, why is this?
    public class Song
    {
     
       static Scanner keyboard = new Scanner (System.in);
     
       public static void main(String[] args)
       {
          Double songDuration;
          String songTitle, songRemove;
          String [] songs = new String[10];
          double  [] songDurations = new double[10];
     
          for (int count=1; count<=9; count++)
          {
             System.out.println("Please enter song title number " + count);
             songTitle=keyboard.nextLine();
             songs[count] = songTitle;
          }
     
          for (int count=1; count<=9; count++)
          {
             System.out.println("Song Number " + count + " = " + songs[count]);
     
          }
     
          for (int count=1; count<=9; count++)
          {
             System.out.println("Please enter duration of song " + songs[count]);
             songDuration=keyboard.nextDouble();
             songDuration=songDurations[count];
     
          }
     
          System.out.println("What song title would you like to remove from the array?");
          songRemove=keyboard.nextLine();


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code is ignoring the read.

    The code skips the read and ends instead
    The posted code doesn't show anything after the read.
    How do you know that the read was skipped? What if the read read an empty line? Did you print out what was read to see? Add a println after the read that shows what was read into the variable.

    This sounds like the Scanner problem when the code calls nextLine() following a call to one of the other next methods which leave the lineend character in the Scanner's buffer.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code is ignoring the read.

    Yes I know, but I am testing as I go along, I know (well, I think) it has ignored it because the console displays the question then a space then 'Process finished with exit code 0'. It does not let me enter info under the question. Just so that we are both on the same boat, I am talking about this
     
          System.out.println("What song title would you like to remove from the array?");
          songRemove=keyboard.nextLine();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code is ignoring the read.

    Print out the contents of songRemove after the read to see what is in it. I suspect it will be an empty String.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code is ignoring the read.

    Yes it just creates an extra empty space .

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code is ignoring the read.

    That's the Scanner problem when the code calls nextLine() following a call to one of the other next methods which leave the lineend character in the Scanner's buffer.
    You need to call nextLine() one time to read the lineend character that was left in the buffer before trying to read another line.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code is ignoring the read.

    Could you explain this in a more simple way, sorry

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code is ignoring the read.

    Add an extra call to nextLine() after a call to nextDouble() and before a call to the nextLine() method where you want to read a line.
    The extra call to nextLine() will read the endline character left in the buffer from when the user pressed Enter to give data to the nextDouble() method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code is ignoring the read.

    Thank you

Similar Threads

  1. Comparing two log files ignoring the lines
    By Rajitha in forum Java Theory & Questions
    Replies: 1
    Last Post: July 16th, 2012, 11:00 AM
  2. Ignoring cases
    By noel222 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 11th, 2012, 06:34 PM
  3. new to java.... having a hard time trying to read code
    By Newbie_96 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 6th, 2011, 01:51 AM
  4. why does this code read an html file uncontinuously
    By nasi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 25th, 2010, 09:09 PM
  5. Are We seriously Ignoring NetBeans?
    By javacrazed in forum Java IDEs
    Replies: 4
    Last Post: November 12th, 2008, 05:08 PM