0

How would one make an if statement that would take user input and then take that word I asked them to use and choose between either the uppercase or lowercase version and still run the same line of code?

import java.util.Scanner;
//text game practice

public class textGame {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int goblin;
    int troll;
    int spider;

    System.out.println("Would you like to play as the Mage, Warrior, or Rouge?");
    String line1 = scan.next();

    System.out.println("Okay " + line1 + " what is your name?");
    String name = scan.next();

    if (name.equals("Mage" || "mage")) {
        System.out.println("You are a mage named " + name + " from a small wizardry school in the north.");
    }
    if (name == "Warrior" || "warrior") {
        System.out.println("You are a warrior named " + name + " fresh out of the new cadets at the Gambleton Outpost.");
    }
    if (name == "Rouge" || "rouge") {
        System.out.println("You are a rouge named " + name + " you were trained by petty theives in the streets of Limburg.");
    }    
    else {
        System.out.println("You did not answer the question correctly please try again.");
    } 
  }
}
Nuwan Alawatta
  • 1,812
  • 21
  • 29
Mike Esch
  • 1
  • 1

1 Answers1

1

Use the equalsIgnoreCase method:

if (name.equalsIgnoreCase("mage")) {
    System.out.println("You are a mage named " + name + " from a small wizardry school in the north.");
}

In your case, it won't compile. Instead of the name.equals("Mage" || "mage") use that:

if (name.equals("mage") || name.equals("Mage")) {
    System.out.println("You are a mage named " + name + " from a small wizardry school in the north.");
}

You can't use the || inside of the equals method but you can split your if clause in 2 different statements.

CodeMatrix
  • 2,124
  • 1
  • 18
  • 30