java - Checking for data in array indexes -


I am trying to create a program that will be read in a file, split that file into an array where " "And then the variable" theOutput "is set as the value of the index in the array. The problem is that the index is always equal to the 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 ("/"); Println (theInput ); 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 will be "coffee: // localhost / bru", so it does not always use index 5 in the array.

If your statement is a different If the nest is nested in, then if (URL.length == 3) only the length of the URL will be 1 if 1. So, you should do something like this:

  If (URL.length == 1) {theOutput = URL [0];} if (URL.length == 2) {theOutput = URL [1]} // etc.   

Or, you can ask theOutput = URL [URL.length-1] to get the last element of the array.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -