0

My assignment is to create a randomly generated number, ask the user to enter a number, then compare the two and show a messagebox telling whether or not they match. This is my code so far...

import javax.swing.*;   //GUI components

public class RandomGuessMatch {

    public static void main(String[] args) {
        Integer random = (1 + (int)(Math.random() * 5)),
                userNum;

        // Get the input
        userNum = JOptionPane.showInputDialog("Enter a number 1 - 5.");

        //Checks to see if numbers match
        boolean matches = (random == userNum);
        JOptionPane.showMessageDialog(null, "The random number is " + random + ". " + "Does it match? " + matches);

    }

}

The only error I'm getting is when I'm trying to get the user input. "Cannot convert from String to Integer". But, I'm having trouble figuring out how to just get the user's number and correlate it to "userNum" so that I can compare it with "random". Any help?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Vaitelias
  • 25
  • 1
  • 5
  • Why are you using Integer instead of int? Integer is just a class wrapper for int, which is necessary mostly if you need to deal with generics. – Dark Squirtings Sep 14 '15 at 20:12

6 Answers6

1

You can convert from String to Integer trough the parse.

int x = Integer.parseInt("1");

Note that this throws an Exception if the String is NaN

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
1

JOptionPane's showInputDialog method returns a String. What you can do is use the resultant string as an argument in the Integer class's constructor:

userNum = new Integer(JOptionPane.showInputDialog("Enter a number 1 - 5."));

Additionally, since you are using Integer objects, you must use the equals method to compare them:

boolean matches = random.equals(userNum);
TNT
  • 2,900
  • 3
  • 23
  • 34
  • OH, I LOVE YOU! It works perfectly. I have a professor who goes through material SO quickly and often doesn't even cover how to do certain things that she assigns as homework, leaving me clueless once I leave the classroom. I try to figure things out on my own as well as I can, but sometimes I'll admit I need a boost. Thanks for the help. And I was wondering about the equals method! I actually was using it in my code at one point, but I wasn't sure which was the correct method to use and kept switching back and forth, lol. – Vaitelias Sep 16 '15 at 14:18
0

You need to cast your string response into an Integer using

Integer.parseInt(userChoice)
andrewdleach
  • 2,458
  • 2
  • 17
  • 25
0

You can get the input this way:

userNum = Integer.getInteger(JOptionPane.showInputDialog("Enter a number 1 - 5."));

Later you can do some input validation to prevent non Integer input.

mjpirez
  • 7
  • 1
  • `Integer.getInteger` is the wrong method here. You should use `Integer.parseInt` or `Integer.valueOf`. `Integer.getInteger` "determines the integer value of the system property with the specified name". He's just trying to parse an integer from a String. – Evan LaHurd Sep 14 '15 at 20:18
  • Thanks, in a hurry just picked the first method from auto-completion. – mjpirez Sep 14 '15 at 20:34
0

ShowInputDialog will return a String. Therefore, the variable that you put its value into must also have type String.

Try making a variable called "userInput" and put the value that the user enters in there. Then you can say

userNum = Integer.valueOf(userInput);

From there, compare the two Integers using "userNum.equals(random)". The double equals operator will only work on int types, not Integers.

0

If you read a user input it is a String, so you have to declare userNum as a String instead an Integer.

String userNum;

The next step is to compare your random number with the String. To do that you need the method Integer.valueOf(String s), so they will be both Integer values:

boolean matches = (random == Integer.valueOf(userNum));

Have fun :)

suggi87
  • 59
  • 1
  • 6