0

I have JUST started with Java and i just want to make a little program, a little game based on luck, where i have to guess a randomly chosen number and whenever i guess it, a window pops up giving me a message.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String haha = jTextField1.getText();
   Random celka = new Random();
   int ciprx = celka.nextInt(6)+1;

   if (haha.contains(ciprx)){
       JOptionPane.showMessageDialog(null, "BUHĀ!");
   }
}   

The problem is that i cant write ciprx after .contains, since that is and integer and i cannot put that there, but i need the text field to contain the (secretly) randomly generated number in case to show me the pop-up message. Its fine, however, if i just put an "a" after the .contains, for example. How can i fix this?

  • 1
    `String.valueOf(ciprx)` – azurefrog Sep 12 '17 at 19:50
  • 1
    Use `equals` instead of `contains`, as if the user types "123456789", and your random number is 345, `contains` will find it.. – Usagi Miyamoto Sep 12 '17 at 19:52
  • 1
    Does this answer your question? [How do I convert from int to String?](https://stackoverflow.com/questions/4105331/how-do-i-convert-from-int-to-string) You really should try to narrow your question down to the smallest possible program (a [mcve]) with a more specific question - if you just want to check whether a string contains an integer, we don't really need to know that you've got a TextField or that you're generating a random number or showing a message - all of that just distracts from the issue. – Bernhard Barker Sep 12 '17 at 19:54

1 Answers1

2

You just need to convert it to a string first:

if (haha.contains(String.valueOf(ciprx)))
shmosel
  • 49,289
  • 6
  • 73
  • 138