-1

I wrote a method that takes a website address and returns a String[], which contains protocol, domain and context (if there is one).
Example: http://stackoverflow.com/questions => {"http", "stackoverflow", "questions"}

String[] splitAddress(String address) {
String[] split = address.split("://");
String[] split1 = split[1].split(".com");
if (split1[1] == "") {
    String[] end = new String[2];
    end[0] = split[0];
    end[1] = split1[0];
    return end;
} else {
    String[] e = new String[3];
    e[0] = split[0];
    e[1] = split1[0];
    e[2] = split1[1];
    return e;
}

It can compile, but when I start this, nothing happens. Where is a mistake?

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Ropoocha
  • 25
  • 5

2 Answers2

0

System.out.println(array) does not work for Arrays. You must write array values to console in a for loop or you can use Arrays.toString(array) method as M. Mariscal wrote below. Code below is working, you need only remove the / character before "/question".

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

        String[] array = splitAddress("http://stackoverflow.com/questions");
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    static String[] splitAddress(String address) {
        String[] split = address.split("://");
        String[] split1 = split[1].split(".com");
        if (split1.length < 2) {
            String[] end = new String[2];
            end[0] = split[0];
            end[1] = split1[0];
            return end;
        } else {
            String[] e = new String[3];
            e[0] = split[0];
            e[1] = split1[0];
            e[2] = split1[1];
            return e;

        }
    }
}

Result:

http
stackoverflow
/questions
rdonuk
  • 3,921
  • 21
  • 39
  • Can lack of `static` modifier in method splitAddress may be the cause? – Ropoocha Mar 16 '16 at 19:19
  • No, cause is you are using System.out.println(array). It is not prints the elements of an array. – rdonuk Mar 16 '16 at 19:20
  • OK. It works if as address we give http://stackoverflow.com/questions, but if we give http://stackoverflow.com, then appear an error: `java.lang.ArrayIndexOutOfBoundsException`. – Ropoocha Mar 16 '16 at 20:00
  • try change `String[] split1 = split[1].split(".com");` to `String[] split1 = split[1].split(".com", -1);` – rdonuk Mar 16 '16 at 20:10
  • And if you need a more complex regex to parse an url, take a look at this http://stackoverflow.com/questions/27745/getting-parts-of-a-url-regex – rdonuk Mar 16 '16 at 20:12
  • But it still returns 3 Strings in String[]. Is it possible to do, that it will be return 2 String if address is without text after `.com`? – Ropoocha Mar 16 '16 at 20:25
0

I think that you are looking for something like this:

 import java.util.Scanner;

    public class Main
    {

        public static void main (String[] args){

     System.out.println(java.util.Arrays.toString(
     splitAddress("http://stackoverflow.com/questions")));
        }

        public static String[] splitAddress(String address) {
        String[] split = address.split("://");
        String[] split1 = split[1].split(".com/");
        if (split1[1] == "") {
            String[] end = new String[2];
            end[0] = split[0];
            end[1] = split1[0];
            return end;
        } else {
            String[] e = new String[3];
            e[0] = split[0];
            e[1] = split1[0];
            e[2] = split1[1];
            return e;
        }
    }
}

finally shows: [http, stackoverflow, questions]

M. Mariscal
  • 1,226
  • 3
  • 17
  • 46