Advertisement
binibiningtinamoran

ReadString

May 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3.  
  4. public class ReadString {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. String str;
  9. Scanner input = new Scanner(System.in);
  10.  
  11. System.out.print("Enter strings, enter DONE when finished: ");
  12. str = input.nextLine();
  13.  
  14. do {
  15. try {
  16. if ((str.length() > 20)) {
  17. throw new StringTooLongException();
  18. }
  19. System.out.print("Enter strings, enter DONE when finished: ");
  20. str = input.nextLine();
  21. } catch (StringTooLongException e) {
  22. // Delete the line below before submitting, it is just a test.
  23. System.out.println("If you see this, then exception is caught, thus this " +
  24. "catch block works.");
  25. System.out.println(e.getMessage());
  26. return;
  27. }
  28. } while(!str.equalsIgnoreCase("done"));
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement