Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Program to test exception handling with Strings
- *
- * @author Kyle Quirk
- *
- */
- import java.util.Scanner;
- public class StringProblem {
- public static void main(String[] args) {
- String word;
- Scanner scan = new Scanner(System.in);
- try{
- System.out.println("Enter name:");
- String name = scan.nextLine();
- if(name.length() < 5) {
- throw new Exception("name is too short -- cannot display fifth letter");
- }
- System.out.println("Fifth letter in name is " + name.charAt(4));
- System.out.println("First letter in word is " + name.charAt(0));
- }
- catch(Exception excpt){
- System.out.println(excpt.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement