Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Lfraction valueOf (String s) {
- if (s.isEmpty() || s == null)
- throw new RuntimeException("Sisend on tühi");
- String[] numbers = s.split("/");
- if (numbers.length != 2)
- throw new RuntimeException("Sisestatud sõne peab sisaldama täpselt kaks arvu, sisendis on: \"" + s + "\"");
- // kas lugejas on arv?
- if (!isNumeric(numbers[0]))
- throw new RuntimeException("Sisestatud murru lugeja ei ole arv: \"" + numbers[0] + "\"" );
- // kas nimetajas on arv?
- if(!isNumeric(numbers[1]))
- throw new RuntimeException("Sisestatud murru nimetaja ei ole arv: \"" + numbers[1] + "\"" );
- return new Lfraction(Long.parseLong(numbers[0]), Long.parseLong(numbers[1]));
- }
- /** Kas string koosneb numbritest?
- * @param str
- * @return boolean
- */
- public static boolean isNumeric(String str) {
- if (str == null) {
- return false;
- }
- int sz = str.length();
- for (int i = 0; i < sz; i++) {
- if (Character.isDigit(str.charAt(i)) == false && str.charAt(i) != '-') {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement