samedi 5 novembre 2016

Java - Not sure why user input is not going into if statement on the first time in the while loop [duplicate]

This question already has an answer here:

I'm new to java and I'm having a hard time trying to figure out why when I select an option from the menu list, the first time it does not actually go into the if statement. For example, when I enter 'o', it just loops and gives the menu choices again. If I select 'o' again, it then goes into the if statement for 'o'. Here is my code:

System.out.print("Enter playlist's title: ");
titleName = listTitle.nextLine();

while (!menu.contains(playlistOption)){
       songs.printMenu();
       playlistOption = listTitle.nextLine().charAt(0);
    }
// begining of menu selection    
while (playlistOption != 'q'){
    songs.printMenu();
    playlistOption = listTitle.nextLine().charAt(0);
    // beginning o menu option 
    if (playlistOption == 'o'){

        System.out.println ("playlist Option =: "); // This was just to see
        // if it was going into the if statement.
        songs.printPlaylistSongs();           
    }
    // end of o menu option        
    //begining of a menu option
    else if (playlistOption == 'a'){
        songs.addSong();

This is my printPlaylistSongs method if you need it:

//Beginnig of printPlaylistSongs method

   public void printPlaylistSongs(){
       System.out.println ("Checking to see if this method actually gets    called");
       System.out.println ("Head value is : " + head);
       if (head == null){
           System.out.println   ("Playlist is empty");            
        }

       else {
           System.out.println ("Unique ID: "    + getID());
           System.out.println ("Song Name: "    + getSongName());
           System.out.println ("Artist Name: "  + getArtistName());
           System.out.println ("Song Length: "  + getSongLength()); 
        }

    }

//End of printPlaylistSongs method

Also if anyone can give me a good link/video on what link list are, how they work, and how to set them up in code it would be helpful. I have looked at few articles on here and other websites, as well as looked at quite a few videos on youtube, but I'm just not grasping the concepts.

Thanks for all your help.

Aucun commentaire:

Enregistrer un commentaire