-1

I have no idea why this doesn't work. Please help, I am new to java

public static void main(String[] args) {
    Scanner scanner  = new Scanner(System.in);
    System.out.println("what is your first number ");
    String firstNum = scanner.nextLine();
    System.out.println("what is your second number ");
    String secondNum = scanner.nextLine();
    System.out.println("what is your Third number ");
    String thirdNum = scanner.nextLine();
    if (firstNum > secondNum) {
        System.out.println("the firs number is bigger than the firs number");
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • You cannot compare String using `>` or `<` operators, there is method `compareTo` of interface `Comparable` that has to be used for this purpose: `if (firstNum.compareTo(secondNum) > 0)` – Nowhere Man Mar 01 '21 at 16:16
  • `String` is a text and your compiler is probably telling you that it has no idea what the > operator is supposed to do with Strings aka Text. If you want to read in numbers, you should really very much consider using a fitting data type like `int`, `long`, `double` etc. pp – OH GOD SPIDERS Mar 01 '21 at 16:17

2 Answers2

1

Scanner.nextLine() is used to input string . Instead use scanner.nextInt() to input number and store in an integer variable.

Harsh Chaturvedi
  • 679
  • 6
  • 13
0

This is an illegal comparison. You cannot compare a String to a String. You should use Scanner.nextInt(). If you want to use a string to store a numerical value than you can use Integer.parseInt() to convert string into int.

suppaGonzalo
  • 107
  • 1
  • 9