0

So i have been trying to help myself learn Japanese by making a randomizer. When I try to run this code it says there are errors but I've looked through and I can't find any. For some reason it doesn't see that the Ename is the same as answer. they are both string variables so I don't understand why it wont work properly. I would love to learn why it isn't working. Thank you for those more knowledgeable than me.

import java.util.Random;
import java.util.Scanner;

public class Game {
Scanner in = new Scanner(System.in);

public int enemyCount = 4; // always need to be one less than the enemy

public String Jname;//Japanese Character
public String Ename;//English Character


public static void main(String[] args) 
{

    new Game();

}

public Game(){

    battleMainMenu();
}

public void RandomEnemy() 
{


    Random random = new Random();

    int rnd = random.nextInt(enemyCount);

    if (rnd == 0)
    {

        Jname= "あ";

        Ename= "a";

    }
    else if (rnd == 1)
    {

        Jname ="い";

        Ename ="i";


    }
    else if (rnd == 2)
    {

        Jname="う";

        Ename="u";

    }
    else if (rnd == 3)
    {

        Jname="え";

        Ename="e";

    }
    else if (rnd == 4)
    {

        Jname="お";

        Ename="o";

    }

    System.out.println("\nYou walk around and encounter a "+ Jname );

}

public void battleMainMenu() 
{
    RandomEnemy();

    System.out.println("\nTo defeat this enemy you must write "+ Jname +"'s name in english. " + Ename);// Enames here for debugging purposes
    System.out.println("Your options are : 'a', 'i', 'u', 'e', 'o' ");
    System.out.println("Type one of the options in lower case. ");

    String answer = in.nextLine();
    System.out.println(answer);
    System.out.println(Ename);
    if (answer == Ename)
    {
        System.out.println("\nCorrect!\n");

    }
    else
    {
        System.out.println("\nIncorrect!\n");
        battleMainMenu();
    }
}

}

Minial
  • 321
  • 2
  • 17
Comary
  • 1
  • 1
  • Btw i did convert it to utf-8 so it can show the characters in the console. – Comary Dec 31 '18 at 03:33
  • How is this relevant to learning Japanese tho – Minial Dec 31 '18 at 03:33
  • I am making it so it randomly gives me a charactor and i answer it in the english letter it sounds like to assist in memorizing so that later i can use it for kanji – Comary Dec 31 '18 at 03:34
  • can you post what the error says – Minial Dec 31 '18 at 03:35
  • It doesn't give an error. but the if (answer == Ename) { System.out.println("\nCorrect!\n"); } else { System.out.println("\nIncorrect!\n"); battleMainMenu(); } //isn't working – Comary Dec 31 '18 at 03:36
  • I can't figuere out why – Comary Dec 31 '18 at 03:38
  • it keeps saying incorrect when it should say correct i put the Print Ename so i know im inputing the right letters – Comary Dec 31 '18 at 03:41
  • Thank you very much. especially for the link. the program now works. – Comary Dec 31 '18 at 03:47
  • Yup, compare strings with the use of `answer.equals(Ename)` instead of using `==` – Minial Dec 31 '18 at 03:48
  • mark his answer as answered thanks~ – Minial Dec 31 '18 at 03:48
  • Not related to your primary question, but you should create a single `static` instance of `Random` in your main, and then re-use it. ***Don't*** create a new `Random` each time you call `RandomEnemy()`. – pjs Dec 31 '18 at 18:49

1 Answers1

1

Use someSting.compare(otherString) to compare strings, not ==. If you want to know why you have to do so look here: How do I compare strings in Java?

In your case:

if (answer.equals(Ename)) {...}
Kami Kaze
  • 611
  • 3
  • 14