Advertisement
ksoltan

JM Chapter 10 #17

Mar 11th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // No description, too long to write out
  2.  
  3. JM Chapter 10 #17
  4.  
  5. "1STFILE.TXT", "1STFILE.TXT"
  6. "HI:IamAllen", null
  7. "blah.blah.blah", null
  8. "HItherewhereareyou", null
  9. "1STFILE", "1STFILE.TXT"
  10. "1stFile", "1STFILE.TXT"
  11. "1stfile.", "1STFILE"
  12. "1STFILE.L", "1STFILE.L"
  13. "1STFILE.l", "1STFILE.l"
  14. "hello!?.txt", null
  15. "h.ello!.txt", "null"
  16. "hello!.jpeg", "null"
  17. "\\hello", "null"
  18.  
  19. private String validFileName(String fileName) {
  20. if (fileName.length() > 11) {
  21. return null;
  22. }
  23. // There are more than two dots in the fileName or the extension is too
  24. // long
  25. if (fileName.indexOf(".") != -1 && (fileName.indexOf(".") != fileName.lastIndexOf(".")
  26. || (fileName.length() - fileName.lastIndexOf(".") > 4))) {
  27. return null;
  28. }
  29. // Check that there are no illegal characters, "." was checked for
  30. // before
  31. if (fileName.contains(":") || fileName.contains("\\")
  32. || fileName.contains("?") || fileName.contains("*")) {
  33. return null;
  34. }
  35. if (fileName.indexOf(".") == fileName.length() - 1) {
  36. return fileName.toUpperCase().substring(0, fileName.length() - 1);
  37. }
  38. if (fileName.indexOf(".") == -1) {
  39. return fileName.toUpperCase() + ".TXT";
  40. }
  41. return fileName.substring(0, fileName.indexOf(".")).toUpperCase()
  42. + fileName.substring(fileName.indexOf("."));
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement