0

very new to java and wondering why this while statement won't end even when the input from the scanner is "N" thus evaluating the expression to false.

import java.util.*;

public class addSongs {

    public static void main (String[]args){

        Scanner songAdd = new Scanner(System.in);
        Scanner addContinue = new Scanner(System.in);
        String ceaseAdd = new String();
        ceaseAdd = "Y";

        while(ceaseAdd != "N")
            System.out.println("Enter track title");
            String newSong = songAdd.nextLine();
            MP3_catalogue.title.add(newSong);
            System.out.println("Enter artist name");
            String newArtist = songAdd.nextLine();
            MP3_catalogue.artist.add(newArtist);
            System.out.println("Enter duration");
            String newDuration = songAdd.nextLine();
            MP3_catalogue.duration.add(newDuration);
            System.out.println("Would you like to add another song? Y/N");
            ceaseAdd = addContinue.nextLine().toUpperCase();
    }

}
DT7
  • 1,615
  • 14
  • 26
user2956624
  • 11
  • 1
  • 3
  • 5
    Compare `String` values with `String`'s `equals` method, not with `==` or `!=`. – rgettman Nov 22 '13 at 19:38
  • 2
    this: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java should help you understand *why* @rgettman's comment is correct. – knoight Nov 22 '13 at 19:41
  • Your code and your description of what is happening do not add up. You say that it runs even when "N" is entered. With this code you would never get to enter anything for `ceaseAdd`, it would just print "Enter track title" for eternity. – James Montagne Nov 22 '13 at 19:54

2 Answers2

2

In addition to the string check already pointed out. missing {}. Properly indented your code looks like this:

while(ceaseAdd != "N")
    System.out.println("Enter track title");
String newSong = songAdd.nextLine();
MP3_catalogue.title.add(newSong);
System.out.println("Enter artist name");

Without {} that while only applies to the first line. It should be this:

while(!ceaseAdd.equals("N")){      
    System.out.println("Enter track title");
    String newSong = songAdd.nextLine();
    MP3_catalogue.title.add(newSong);
    System.out.println("Enter artist name");
    String newArtist = songAdd.nextLine();
    MP3_catalogue.artist.add(newArtist);
    System.out.println("Enter duration");
    String newDuration = songAdd.nextLine();
    MP3_catalogue.duration.add(newDuration);
    System.out.println("Would you like to add another song? Y/N");
    ceaseAdd = addContinue.nextLine().toUpperCase();
}
James Montagne
  • 77,516
  • 14
  • 110
  • 130
0

in java you don't compare strings with "==". "==" checks if it is exact the same object and not if it has the same value. To check if it has the equal text, you have to use

string.equals()

In your example it would look like this:

public static void main (String[]args){

Scanner songAdd = new Scanner(System.in);
Scanner addContinue = new Scanner(System.in);
String ceaseAdd = new String();
ceaseAdd = "Y";

while(!ceaseAdd.equals("N"))
    System.out.println("Enter track title");
    String newSong = songAdd.nextLine();
    MP3_catalogue.title.add(newSong);
    System.out.println("Enter artist name");
    String newArtist = songAdd.nextLine();
    MP3_catalogue.artist.add(newArtist);
    System.out.println("Enter duration");
    String newDuration = songAdd.nextLine();
    MP3_catalogue.duration.add(newDuration);
    System.out.println("Would you like to add another song? Y/N");
    ceaseAdd = addContinue.nextLine().toUpperCase();
}
Pinguin895
  • 999
  • 2
  • 11
  • 28