LoginSignup
1
1

More than 5 years have passed since last update.

JAVA split()/StringBuilder/endsWith/matches/文字コード/練習問題P49

Last updated at Posted at 2015-11-24

■split()

public class Test01 {
    public static void main(String[] args) {
        String s = "abc,def:ghli,jk:ll";
        String[] words = s.split("[,:l]");
        for (String w : words) {
            System.out.println(w);
        }
    }

}

■実行結果
abc
def
gh
i
jk

■printf

public class Test01 {
    public static void main(String[] args) {
        System.out.printf("製品番号%s-%02d", "SJV", 3);
        System.out.printf("テスト%04f-%04d", 1.23 , 4);
    }
}

■実行結果
製品番号SJV-03テスト1.230000-0004

1.7 練習問題

■練習問題1-1 P49 解答

public class Test01 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            sb.append(i + 1).append(",");
        }
        String s = sb.toString();
        String[] a = s.split("[,]");
        System.out.println(a);
        System.out.println(s);

    }
}

■練習問題1-1 P49 解答
[Ljava.lang.String;@19e0bfd
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,

■String配列aに格納しなくても出来る方法

public class Test01 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            sb.append(i + 1).append(",");
        }
        String s = sb.toString();
        // String[] a = s.split("[,]");
        // System.out.println(a);
        System.out.println(s);

        StringBuilder sb1 = new StringBuilder();
        for (int i1 = 0; i1 < 100; i1++) {
            sb1.append(i1 + 1).append("/");
        }
        String s1 = sb1.toString();
        System.out.println(s1);

    }
}

■String配列aに格納しなくても出来る方法 実行結果
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,
1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100/

■練習問題1-2 P49 解答

public class Test01 {
    public static void main(String[] args) {
        test("cccfff", "aaa.txt");
    }

    static String test(String folder, String file) {
        if (!folder.endsWith("\\")) {
            folder += "\\";
            System.out.println("フォルダ名は" + folder + "ファイル名は" + file);
        }
         return folder + file;

    }

}

■練習問題1-2 P49(解答ではないが、これでも同じ結果が出せる)

public class Test01 {
    public static void main(String[] args) {
        test("cccfff", "aaa.txt");
    }

    static String test(String folder, String file) {
        if (!folder.endsWith("\\")) {
            folder += "\\";
            System.out.println("フォルダ名は" + folder + "ファイル名は" + file);
        }
        // return folder + file;
        return folder;

    }

}

■練習問題1-2 実行結果
フォルダ名はcccfff\ファイル名はaaa.txt

■練習問題1-2 P49 解答②

public class Test01 {
    public static void main(String[] args) {
        System.out.println(test("完全なファイル名は" + "nnnnnddd", "jjj.txt"));
        System.out.println(test("xxxxyyy", "yyysss.txt"));

    }

    static String test(String folder, String file) {
        if (!folder.endsWith("\\")) {
            folder += "\\";

        }
        return folder + file;

    }

}

■練習問題1-2 P49 解答② 実行結果
完全なファイル名はnnnnnddd\jjj.txt
xxxxyyy\yyysss.txt

matches()メソッド

条件
・何桁でもいい
・英字を使わないといけない→.[a-zA-Z].を使う

public class Test02 {
    public static void main(String[] args) {
        System.out.println("条件:何桁でもいい。英字を使わないといけない。");
        System.out.println(test8("ARUKJOPW"));
        System.out.println(test8("ARUKJOPWW"));
    }

    static String test8(String folder) {

        if (!folder.matches(".*[a-zA-Z].*")) {
            String x = "エラーです。";
            return x;
        } else {
            return folder;
        }
    }

}

■実行結果
条件:何桁でもいい。英字を使わないといけない。
ARUKJOPW
ARUKJOPWW

.[a-zA-Z].(例)

public class MatchesTest {
    public static void main(String[] args) {
        testFolderName("A");
        testFolderName("ARUKJOPW");
        testFolderName("Test");
        testFolderName("テストTestテスト");
        testFolderName("*Test*");

        testFolderName("あ");
        testFolderName("!");
        testFolderName("テスト");
    }

    static void testFolderName(String folderName) {
        if (isValidFolderName(folderName)) {
            System.out.println("[" + folderName + "] is valid(正当).");
        } else {
            System.out.println("[" + folderName + "] is NOT valid.");
        }
    }

    static boolean isValidFolderName(String folderName) {
        return folderName.matches(".*[a-zA-Z].*");
    }
}

■実行結果
[A] is valid(正当).
[ARUKJOPW] is valid(正当).
[Test] is valid(正当).
[テストTestテスト] is valid(正当).
[Test] is valid(正当).
[あ] is NOT valid.
[!] is NOT valid.
[テスト] is NOT valid.

char型の1文字を文字コードに変換


public class Test06 {
    public static void main(String[] args) {
        char i = 'D';
        char i2 = 'B';
        int code = (int) i;
        System.out.println(code);
        int code2 = (int) i2;
        System.out.println(code2);
        System.out.println(code + code2);
        System.out.println(i2);
    }

}

■実行結果
68
66
134
B

文字コード

■文字コードとは
文字コードとは、文字に1対1で対応する番号をつけて表現する方法のことです。
例えば、半角の A には65番が割り当てられています。
char型変数に文字を代入するのは、この番号を代入しているだけのことであり、
要するに char型は普通の整数型と全く同じなのです。
charは16bitで文字を表現しています。

■まとめ例
■Test01.java

public class Test01 {
    public static void main(String[] args) {
        System.out.println("完全なファイル名のルールは「c:\\xxx\\zzzzz.txt」などの様な形にしないといけない。");
        System.out.println(test("nnnnnddd", "jjj.txt"));
        System.out.println("完全なファイル名は" + test("nnnnnddd", "jjj.txt"));
        System.out.println(test("xxxxyyy", "yyysss.txt"));
        System.out.println(test2("rrrrzzz", "sssskkk"));
        System.out.println("冒頭がAでないといけない時の完全なファイル名は"
                + test2("rrrrzzz", "sssskkk"));
        System.out.println(test3("aa"));
        System.out.println(test3("fffff"));
        System.out.println(test3("aff0"));
        System.out.println("フォルダ名はAからZまでの文字しか使えない→" + test4("00000"));
        System.out.println(test4("A"));
        System.out.println("フォルダ名はA-Zまでの文字しか使えないかつ5文字でないといけない→"
                + test5("AAAAS"));
        System.out.println(test5("AFFFX"));
        System.out.println("フォルダ名はA-Zまでの文字しか使えないかつ10文字でないといけない→"
                + test6("FFFFDEWSSL"));
        System.out.println(test8("ARUKJOPW"));
        System.out.println(test9("ALLLLBCBCC"));
        System.out.println(test9("ACCCCCCDDE"));
        System.out.println(test10("Ja"));
        System.out.println("すべての文字を許す→" + test11("eee"));
    }

    static String test(String folder, String file) {
        if (!folder.endsWith("\\")) {
            folder += "\\";

        }
        return folder + file;

    }

    static String test2(String folder, String file) {
        if (!folder.startsWith("A")) {
            folder = "A" + folder;
        }
        return folder + file;
    }

    static String test3(String folder) {
        if (folder.length() != 5) {
            System.out.println("エラー:フォルダー名は5文字です。");
            String x = "エラーなので、フォルダー名を直してください。";
            return x;
        } else {
            return folder;
        }
    }

    static String test4(String folder) {

        if (!folder.matches("[A-Z]")) {
            String x = "エラーです。";
            return x;
        } else {
            return folder;
        }
    }

    static String test5(String folder) {
        for (int i = 1; i < 5; i++) {
            char c = folder.charAt(i);
            if (!(c >= 'A' && c <= 'Z')) {
                String x = "エラーです。";
                return x;
            }
        }
        return folder;
    }

    static String test6(String folder) {
        for (int i = 1; i < 10; i++) {
            char c = folder.charAt(i);
            if (!(c >= 'A' && c <= 'Z')) {
                String x = "エラーです。";
                return x;
            }
        }
        return folder;
    }

    static String test8(String folder) {

        if (!folder.matches(".*[a-zA-Z].*")) {
            String x = "エラーです。";
            return x;
        } else {
            return folder;
        }
    }

    static String test9(String folder) {
        for (int i = 1; i < 10; i++) {
            char c = folder.charAt(i);
            if (!(c >= 'A' && c <= 'K')) {
                String x = "エラーです。";
                return x;
            }
        }
        return folder;
    }

    static String test10(String folder) {
        if (!folder.matches("Ja*")) {
            String x = "エラーです。";
            return x;
        } else {
            return folder;
        }
    }

    static String test11(String folder) {
        if (!folder.matches(".*")) {
            return folder;
        } else {
            return folder;
        }
    }

}

■Test01.java 実行結果
魚は5種類
black rockfishメバル
globefishフグ
porcupinefishハリセンボン
yellowtailブリ
codタラ

拡張for文
black rockfishメバル
globefishフグ
porcupinefishハリセンボン
yellowtailブリ
codタラ

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1