ふわ

2021年04月04日に参加

学習履歴詳細

【5回目】問題3の復習/問題4の回答

学んだこと

  • 問題3で、空白でスプリットする際に、WHITE_SPACEの定数として括り出していた。
  • Stream APIを学ばないとなと思った。
  • 変数を命名する際、その変数の中に何が入っているのかわかりやすく書く必要があるなと思った。
    • 文章を逆の文字列、並びにして返す関数の場合
    • 返却用のローカル変数をresultと書くのではなく、reversedStringと書くなど。
  • String.parseInt()メソッドのJavaソースを読んでみた。頑張れば理解できなくもないかも。
  • 問題4で、文字列が数字だけでできているかを確かめる際、String.parseIntを使用するのは悪手らしい。次回要確認。
package S_4_myAnswer;
//Checking whether a string contains only digits: Write a program that checks whether the given string contains only digits.
//
//Leonard, Anghel. Java Coding Problems: Improve your Java Programming skills by solving real-world coding challenges (p. 9). Packt Publishing. Kindle Edition.

public class Main {
    public static void main(String[] args) {

        //String testStr = "1000243";
        String testStr = "1000243";
        //System.out.println(isNumeric(testStr));
        System.out.println(isAllNumeric(testStr));
    }

    public static boolean isAllNumeric(String str){
        if (str == null) {
            return false;
        }

        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {
                continue;
            } else {
                return false;
            }
        }

        return true;
    }

//    public static boolean isNumeric(Char chr){
//        if(chr == null) {
//            return false;
//        }
//        try {
//            int i = Integer.parseInt(str);
//        } catch (NumberFormatException e) {
//            return false;
//        }
//        return true;
//
//    }
}
Java

2023年06月20日(火)

1.5時間