Shreyas arya

Generation

generate functionFri, 10 Feb 2023

Create a method print a string If type is not string then throw exception and retrun exception and retry 3 times

import java.util.Scanner; public class ExceptionDemo { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a string: "); String str = scanner.next(); int len = str.length(); System.out.println("The length of the string is: " + len); } catch (Exception e) { System.out.println("Error occurred: " + e.getMessage()); } } }

Questions about programming?Chat with your personal AI assistant