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

Thread: Can anyone see whats wrong with this code

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

    Default Can anyone see whats wrong with this code

    Why is this code not printing out the print statement when I enter a name inside songRemove that matches the array.
      System.out.println("What song title would you like to remove from the array?");
          songRemove=keyboard.nextLine();
     
          keyboard.nextLine();
     
          for(int count=0; count<=9; count++)
          {
     
            if (songRemove == songs[count])
            {
               System.out.println("You want to remove" + songs[count]);
            }
     
          }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Can anyone see whats wrong with this code

    You should use the equals() method to compare the content of objects like String , not the == operator.

    What is the second call to the nextLine() method for? The String it returns is not being saved.
    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: Can anyone see whats wrong with this code

    Still does not fix the problem, heres my code now
    System.out.println("What song title would you like to remove from the array?");
          songRemove=keyboard.nextLine();
     
          keyboard.nextLine();
     
          for(int count=0; count<=9; count++)
          {
     
            if (songRemove.equals(songs[count]))
            {
               System.out.println("You want to remove" + songs[count]);
            }
     
          }

    That fixes the problem I had here:
    Code is ignoring the read.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Can anyone see whats wrong with this code

    Put that second, "unused" call to nextLine() in a println() to see what it reads.

    Also printout the value of songRemove to see what its value is.
    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: Can anyone see whats wrong with this code

    put it in a print ln as in System.out.println(keyboard.nextLine());??

    ps. i discovered songRemove has no value, is this because of this 'unused' call?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Can anyone see whats wrong with this code

    songRemove has no value
    Read the posts in Code is ignoring the read.

    The problem is discussed there.

    put it in a println as in
    System.out.println("nL="+keyboard.nextLine() +"<");??
    What was printed when you did that?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Im not sure whats wrong with my code
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 25th, 2013, 09:08 PM
  2. help me with a code, whats wrong?
    By Heizzer10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2012, 10:09 AM
  3. Can someone see whats wrong with my code, please?
    By DJBENZ10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 11th, 2012, 08:54 PM
  4. Whats wrong with my code!!
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2011, 11:45 AM