How can I set an integer equal to a String?

Im making a project in Java and I wanted to make an integer equal a string in an if statemtent. Is that possible? If so how?

1 Like

That doesn’t make sense, take a step back and figure out what you want to accomplish.

Perhaps you mean to test whether a piece of text matches the textual representation of some number, in which case, you’d probably want to obtain the textual representation of that number and then compare that to your string, which would be a comparison between two strings.

If you want to print it out then you can just do something like:

public class Number{
  public static void main(String[] args){
    int number = 5;
    System.out.println(number);
  }
}

if you want to join it to a string then:

public class Number{
  public static void main(String[] args){
    int number = 5;
    String message = "This number is: ";
    String fullMessage = message + number;
    System.out.println(fullMessage);
  }
}

The idea of an integer and a string are very similar, tell me if this helps!