I am trying to create a program that will read in a file, split that file into an array wherever there is an "/" and then have the variable "theOutput" set as the value of the index in the array. Trouble is, the index is always equal to null. Here is my code:
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];
}
}
}
An example of the data found in the file would be "coffee://localhost/brew" so it doesn't always use 5 indexes in the array.
URL[URL.length - 1]to get the last element of the array.Array.length - 1.URL[0]. if the length is 2, you assign output toURL[1]. So the index is always one less than the length of the array, sotheOutput =URL[URL.length - 1]` is sufficient