Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. /**
  2.  * Program to test exception handling with Strings
  3.  *
  4.  * @author Kyle Quirk
  5.  *
  6.  */
  7. import java.util.Scanner;
  8.  
  9. public class StringProblem {
  10.    
  11.     public static void main(String[] args) {
  12.         String word;
  13.         Scanner scan = new Scanner(System.in);
  14.  
  15.         try{
  16.             System.out.println("Enter name:");
  17.             String name = scan.nextLine();
  18.  
  19.             if(name.length() < 5) {
  20.                 throw new Exception("name is too short -- cannot display fifth letter");
  21.             }            
  22.             System.out.println("Fifth letter in name is " + name.charAt(4));
  23.             System.out.println("First letter in word is " + name.charAt(0));
  24.         }
  25.         catch(Exception excpt){
  26.             System.out.println(excpt.getMessage());
  27.         }
  28.  
  29.    }
  30.      
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement