I'm trying to write a code for a program that recives strings as an input. The program prints "Error" when the user does not put any data, otherwise it prints the first string argument.
Is it right to refer to no data as a "null"? It does not work. what should I write instead?
public class Try {
public static void main(String[] args){
if (args[0]==null){
System.out.println("Error- please type a string");
}else {System.out.println(args[0]);}
}
}
Arguments will never be null
if they exist in the first place -- to check that, you should use args.length
instead:
if (args.length == 0) {
...
} else {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With