CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2005
    Posts
    37

    Play for Ozzy (my program is not working correctly)

    I have this cool program that uses a logical operator to join two conditions in an if statement. Basically for a person to get a job playing for Ozzy Osbourne you have to play a LesPaul and you have to have long hair. I did this program in C++ and I think it's cool so I wanted to transpose it over to a Java program.

    But for some reason the statement in the if part does not run. Even if I enter LesPaul and Long the program wants to skip the statement that says "Congratulations you've got the gig!", and it wants to always print "Sorry you do not qualify for this gig".

    I don't know why it wants to do that.

    Here is my program:
    Code:
    import java.util.Scanner;
    
    public class PlayForOzzy
    {
    	public static void main(String[] args)
    	{
    		String guitar = "LesPaul";
    		String hairStyle = "Long";
    		String personsGuitar;
    		String personsHairStyle;
    		
    		@SuppressWarnings("resource")
    		Scanner keyboard = new Scanner(System.in); 
    		
    		System.out.print("Enter the model of guitar you play: ");
    		personsGuitar = keyboard.nextLine();
    			
    		System.out.print("Enter in your hairstyle: ");
    		personsHairStyle = keyboard.nextLine();
    				
    		if(personsGuitar == guitar && personsHairStyle == hairStyle)
    			System.out.print("Congratulations! You got the gig!");
    		else
    			System.out.print("Sorry...you do not qualify for this gig");
    	}
    }
    I must be close but something is off somewhere.

  2. #2
    Join Date
    Mar 2005
    Posts
    37

    Re: Play for Ozzy (my program is not working correctly)

    I tested to see what was contained in the personsGuitar variable after that line executed and indeed LesPaul was in that variable. I also tested to see what was in the personsHairStyle variable after that line executed and again the correct value was in there. The word Long was in there.

    I'm started to suspect the problem may be with the keyboard.nextLine( ) thing. Maybe when I hit the Enter key to move forward one last character is added to the variable or something.

  3. #3
    Join Date
    Nov 2018
    Posts
    120

    Re: Play for Ozzy (my program is not working correctly)

    Or just that you're assuming == does what you want.
    Code:
    import java.util.Scanner;
    
    public class PlayForOzzy
    {
    	public static void main(String[] args)
    	{
    		String guitar = "LesPaul";
    		String hairStyle = "Long";
    		String personsGuitar;
    		String personsHairStyle;
    		
    		@SuppressWarnings("resource")
    		Scanner keyboard = new Scanner(System.in); 
    		
    		System.out.print("Enter the model of guitar you play: ");
    		personsGuitar = keyboard.nextLine();
    			
    		System.out.print("Enter in your hairstyle: ");
    		personsHairStyle = keyboard.nextLine();
    
    		if(personsGuitar.equals(guitar) && personsHairStyle.equals(hairStyle))
    			System.out.print("Congratulations! You got the gig!");
    		else
    			System.out.print("Sorry...you do not qualify for this gig");
    	}
    }
    == just tells you whether you have the same object instance.

  4. #4
    Join Date
    Mar 2005
    Posts
    37

    Re: Play for Ozzy (my program is not working correctly)

    I found out that you don't want to use == to compare strings in Java. You need to use .equals method or the .compare method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured