My Java code isn't executing print statements when the user inputs an option they want to choose. Why is it not working and how do I fix it?

Sarrah Sodawala

I'm creating a game, and the code that I want Java to run isn't running even when the user inputs a valid answer and I'm not sure how to fix the problem.

I tried creating a keyboard statement for the variable and seperately tried a static String variable name for after the public class, but instead it gives me errors when I originally had none.

This is my original code below:

                public static void carMethod (Scanner keyboard) 
    { 
                System.out. println ("Now, what is it that you decide to do?"); 
                System.out.println ("Here are your options:"); 
        System.out.println (); 
        System.out.println (); 

        System.out.println ("(A.) Go up to the car and see if anyone’s inside.");  
        
        System.out.println ("It’s weird enough that a car is in your area anyway"); 
        
        System.out.println (); 
        System.out.println ("OR"); 
        System.out.println (); 
        
        System.out.println ("(B.) Ignore it. It’s obviously none of your business."); 
        
        System.out.println ("So, what’s it going to be?"); 
        
        System.out.println ("A or B?"); 
        System.out.println (); 

        String carSelection=keyboard.nextLine(); 
        
        if (carSelection == "A" || carSelection == "a") { 

            System.out.println ("You peep into the passenger's side’s windows and don’t notice much"); 
    
            System.out.println ("other than used soda cans and chip bags scattered across the floor."); 
            
            System.out.println ("You also notice that the driver’s sides’s windows are completely open");
            
            System.out.println ("and the keys are casually sitting on top of the driver’s seat, waiting to be taken."); 
            
            System.out.println ("What do you plan on doing?"); 
            
            TextAdventure.searchMethod (keyboard); //note that this is a seperate method and does not have anything to do with the problem 
        } 
        
        if (carSelection == "B" || carSelection == "b") { 

            System.out.println ("Well my friend…"); 
            
            System.out.println ("...you let the car be and continue on with your day"); 
            
            System.out.println ("and nothing eventful ever happens to you again."); 

            System.out.println ("THE END"); 
            
        } 
} 

Any help would be greatly appreciated! Thanks.

Fletch

When comparing Strings in Java (or any nonprimitive type), you need to use the .equals method rather than using ==. You can read more about why here, but the fix is just to say

if (carSelection.equals("A") || carSelection.equals("a")) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

My code for this newton's cradle isn't working. How do I fix this?

My Array isn't being filled the way I want it to, how would I fix this?

Why Isn't my Swift code working?

Why is this code not working, How do i fix it?

Why isn't my code working inside a function I created, but works when I run it in the global environment?

Why my Three.js code isn't working when i test it?

Why isn't my code working when adding .386?

Why isn't my code working properly when looping this array

discord,js confusion, I want to add a status to my discord bot but my code isn't working properly, how can I make a status for my bot?

Why isn't my pagination code working?

Why isn't my bash function executing when I know that the definition is processed?

Why isn't my java code proceeding past my if statements?

How do I fix my Java code so that it ends when a user types "quit"?

Why isn't utorrent working? How do I remedy this problem?

Why isn't my insert working using prepared statements?

Why isn't my user alert code in Swift working as intended

My code isn't executing

How do I get the count of loans? Why isn't this working?

Canvas- house. Why isn`t the code working, when I copy it to an editor and run it in my browser?

Why ISN'T there a race condition when I do a read-update-write in this java code?

Code executing both if and else statements together how do i fix?

How do I fix it so that my text label isn't cut off at the top when programmatically adding a text label?

How do I fix my code about a script that stopped working?

When I print out my list it prints out null for all my values..... How do I fix this issue???? I want to learn how to fix this issue for future

Why isn't my function save_users(): working? How do I save these passwords to file?

When I use fetch my .then code isn't working

I want my Assembly Code to takes user input and outputs it along with other text but the output isn't correct

My code isn't printing any of the while loop when I give it valid input. How do I fix?

When I print from this code it returns as nonenone, Why does it do that and how can i fix it?