Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Part2Test
  6. {
  7. public static void main(String[] args)throws InvalidStringException, IOException {
  8.  
  9. File file = new File("test.txt");
  10. Scanner input = new Scanner(file);
  11.  
  12. String regEx = ".*\\d+.*";
  13. String process;
  14.  
  15. while(input.hasNext()){
  16. try {
  17. process = input.next();
  18.  
  19. if(process.matches(regEx))
  20. throw new InvalidStringException(process, "Invalid Character Found!");
  21.  
  22. if(process.length() > 20)
  23. throw new InvalidStringException(process, "Invalid! String excedes 20 characters", process.length());
  24. } //end try
  25. catch(Exception e) {
  26. System.out.println(e.getMessage());
  27. }//end catch
  28.  
  29. }//end while
  30. input.close();
  31. }
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. ////////////////////////////////////////////////////////////////////////////////////
  44.  
  45.  
  46. public class InvalidStringException
  47. extends Exception {
  48.  
  49. public InvalidStringException(String input, String err) {
  50. super(err);
  51. }
  52.  
  53. public InvalidStringException(String input, String err, int size) {
  54. super(err);
  55. if(size > 20)
  56. throw new IllegalArgumentException();
  57. }
  58.  
  59. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement