-1

I'm trying to make a choose-your-own-adventure program and am on my first command. For some reason, despite no errors being found, when I type out one of the options either nothing happens or "dash: 2: Yes: not found" pops up. What am I doing wrong?

My code btw:

import java. util.*;
public class Choose_Your_Own_Adventure {
    public static void main(String[] args) 
    {
        System.out.println("The Scarlet Letter, a Choose Your Own Adventure");
        System.out.println("Rules: Choose either one of the two options provided, in verbatim to letter or one word command only. Ex. A, B, Yes, No, etc.");
        Scanner wordin = new Scanner (System.in);
        System.out.println("Who is the adventurer today? Enter Name Here:");
        String adventurersName = wordin.nextLine();
        System.out.println("Dear,"+ adventurersName);
        System.out.println("Groggy, tired, and disgruntled after a long day at work as a corporate slave, you pull up to your driveway, parking your car sloppily before getting out and walking off to check your mailbox. You have been waiting weeks for the new game you ordered to arrive. With no work to stop you and your weekend schedule as bare as a blank canvas, you are filled with renewed energy and rush to your postal. ");
        
        
         System.out.print("Proceed? (Yes or No)");
          String commandOne = wordin.nextLine();
         if(commandOne == "No"+"no"){
            System.out.println("The End. Boo finish the story properly.");
        }
        else if (commandOne == "Yes"+"yes"){
            System.out.println("You bust open your mailbox expecting to see a big brown package, but instead, a small scarlet envelope. In frustration you? A.Allow yourself a moment to breath or B.You throw the letter to the curb. Slamming the door shut, you rush into your house in a mini temper tantrum and decide to sleep it off.");
            
            String commandTwo = wordin.nextLine();
            if(commandTwo == "B" + "B." + "b"){
                System.out.println("The End");
            }
            else if(commandTwo=="A"+"A."+"a"){
                System.out.println("the begging");
            }
        }
        
        
            }
}
  • I suggest writing a short routine that displays a formatted version (like in hexadecimal) of each of the characters that `wordin.nextLine()` put into `commandOne`. You may find that the characters include the Carriage Return or Line Feed that the user pressed after "No". If so, your code may need to remove the extra characters, or perhaps use a different routine that removes them for you. (I don't know Java, so I don't have a specific suggestion) –  Jun 24 '23 at 19:06
  • Don't use `==` when comparing strings, use `String#equals` instead. – Alex R Jun 25 '23 at 00:08

1 Answers1

0

You'll need to compare the String values using String#equals.

if(commandOne.equals("No") || commandOne.equals("no"))

Additionally, there is the String#equalsIgnoreCase method.

if(commandOne.equalsIgnoreCase("no"))

Or, you could just lower-case the commandOne value, before using equals.

String commandOne = wordin.nextLine().toLowerCase();
if(commandOne.equals("no"))
Reilas
  • 3,297
  • 2
  • 4
  • 17