ファイルを読み取り、そのファイルを「/」がある場所で配列に分割し、変数「theOutput」を配列内のインデックスの値として設定するプログラムを作成しようとしています。問題は、インデックスが常に null に等しいことです。これが私のコードです:
String theOutput = null;
String content = new Scanner(new File("URL.txt")).useDelimiter("\\Z").next();
theInput = content;
String[] URL = theInput.split("/");
System.out.println(theInput);
System.out.println(URL.length);
if (URL.length == 1) {
theOutput = URL[0];
if (URL.length == 3) {
theOutput = URL[2];
if (URL.length == 4) {
theOutput = URL[3];
if (URL.length == 5) {
theOutput = URL[4];
if (URL.length == 6) {
theOutput = URL[5];
}
}
}
ファイルで見つかったデータの例は「coffee://localhost/brew」であるため、配列で常に 5 つのインデックスを使用するとは限りません。