I've searched a lot but found nothing that can solve this problem. I'm using nextInt to read the input from the user, but if the user enters nothing (just presses enter), it will loop infinitely until the user enters something. But what I want is to stop the program if the user enters nothing, and tell them "this is empty". What should I do? Could someone help me please? Thanks in advance.
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
if (scan.hasNextInt()){
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int Difference = Math.abs(num2 - num1);
if (num1 == num2) {
System.out.println("They are equal");
}else if (num1 < 0 | num2 < 0){
System.out.println("No negative number!");
}else{
System.out.println("Their difference is " + Difference);
}
}else{
System.out.println("Wrong! Please enter correct number");
}
}
}