Junior — Middle
How do you verify the success of data reception from the user?
sobes.tech AI
Answer from AI
To verify the successful receipt of data from a user in Java, usually several steps are performed:
- Check that the data is not null and not empty (if it is a string).
- Validate the data format (for example, number, email, date).
- Handle exceptions during data conversion (for example, NumberFormatException when parsing a number).
Example of checking console input for a number:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
if (scanner.hasNextInt()) {
int number = scanner.nextInt();
System.out.println("You entered the number: " + number);
} else {
System.out.println("Error: not a number entered.");
}