0

I am a beginner and I am trying to use things that I have learned so far. I wrote this code and supposedly must return the message "You have found the lucky number" if the number you give it's one of the numbers array. I used the String.valueOf() method so I can get the hang of it.

The problem is, even if I give a number that is in the array I get the message "YOU HAVEN'T FOUND THE LUCKY NUMBER". I can't understand what is wrong.

Thank you.

import java.io.*;

class GameHelper 
{
    public String getUserInput(String prompt){
        String inputLine = null;
        System.out.print(prompt +" ");
        try {
            BufferedReader is = new BufferedReader (
            new InputStreamReader(System.in));
            inputLine = is.readLine();
            if (inputLine.length() == 0) return null;
    } catch (IOException e) {
        System.out.println("IOException: " + e);
      }
        return inputLine;
  }
}

class IntArray
{
    int[] numbers={1,9,8,5,6};
    String result="YOU HAVEN'T FOUND THE LUCKY NUMBER";

    String joker(String guess)
    {

        for(int i=0;i<numbers.length;i++)
        {
            if (guess==String.valueOf(numbers[i])){
            result="You have found the lucky number";
            break;
            }
        }
        return result;
    }

}   


class ProjectMain
{
  public static void main (String[] args)
  {
    GameHelper helper = new GameHelper();
    IntArray player1 = new IntArray();
    String x = helper.getUserInput("enter a number ");
    System.out.println(player1.joker(x));       
  }
}
Eleni Ioakim
  • 53
  • 1
  • 7

1 Answers1

1

Since it's a string, you should use equals

guess.equals(String.valueOf(numbers[i]))
isaace
  • 3,336
  • 1
  • 9
  • 22