Advertisement
UniQuet0p1

Untitled

Oct 15th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. public static Lfraction valueOf (String s) {
  2.  
  3. if (s.isEmpty() || s == null)
  4. throw new RuntimeException("Sisend on tühi");
  5.  
  6. String[] numbers = s.split("/");
  7.  
  8. if (numbers.length != 2)
  9. throw new RuntimeException("Sisestatud sõne peab sisaldama täpselt kaks arvu, sisendis on: \"" + s + "\"");
  10.  
  11. // kas lugejas on arv?
  12. if (!isNumeric(numbers[0]))
  13. throw new RuntimeException("Sisestatud murru lugeja ei ole arv: \"" + numbers[0] + "\"" );
  14.  
  15. // kas nimetajas on arv?
  16. if(!isNumeric(numbers[1]))
  17. throw new RuntimeException("Sisestatud murru nimetaja ei ole arv: \"" + numbers[1] + "\"" );
  18.  
  19. return new Lfraction(Long.parseLong(numbers[0]), Long.parseLong(numbers[1]));
  20. }
  21.  
  22. /** Kas string koosneb numbritest?
  23. * @param str
  24. * @return boolean
  25. */
  26. public static boolean isNumeric(String str) {
  27. if (str == null) {
  28. return false;
  29. }
  30. int sz = str.length();
  31. for (int i = 0; i < sz; i++) {
  32. if (Character.isDigit(str.charAt(i)) == false && str.charAt(i) != '-') {
  33. return false;
  34. }
  35. }
  36. return true;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement