0

Problem is having is I have a string value i read from a line of a file, and then a string value from user input, however when i have user. input as the same as the other value, it wont equal eachother, basically even though the strings equal eachother, for some reason inputWord == word isn't true. I am suffering heres my code:

package all;

import java.io.*;
import java.util.Scanner;
public class main1 {
    static String word = "";
    static int repeater = 0;
    public static void main(String[] args) throws IOException {
        // Create random number 0 - 99
        double randNumber = Math.random();
        double d = randNumber * 100;

        // Type cast double to int
        int randomInt = (int) d;

        BufferedReader reader = new BufferedReader(new FileReader("words.txt"));
        String line;
        int lineindex = 0;
        while ((line = reader.readLine()) != null) {
            if (lineindex == randomInt) {
                word = line;
            }
            lineindex++;
        }
        reader.close();
        System.out.println(word);
        while (repeater == 0) {
        for (int i = 0; i < 5; i++) {
            inputWords();
        }
        }
    }

    static void inputWords() {
        Scanner scan = new Scanner(System.in);
        while (repeater == 0) {
        System.out.println("Enter word: ");
        String inputWord = scan.nextLine();
        if (inputWord == word) {
            repeater = 1;
            System.out.println("Correct, the word was " + word);
        }
        }
        scan.close();
    }
}

  • my post was associated with another post and i figured it out, instead of using inputWord == word, i used inputWord.equals(word) and it worked, idk how i didn't know that, was killing me. – Bread Bread Mar 09 '22 at 10:36

0 Answers0