Java Separate Source Code

Filename:
Separate.java

Source Code: (you can also see this text in Separate.txt)

class Separate {//begin class
public static void main(String args[]) {//begin main

/**This program separates a String fed into it as
//command line parameters and then separates the
//different words by a second command line
//parameter, meaning you type
//"java Separate first,second,third,fourth,fifth ,"
//and it separates the words by the comma which
//is the second command line parameter entered,
//right now it's set up to output how many different
//sections there are, and then putting them
//each out as a separate line */

String temp1 = args[0];
String charSeparator = args[1];

String tempArray1[] = new String[temp1.length()+1];
int temp1Length = temp1.length();
int tempArray1Length = 0;
int tempArray2Length = 0;
int location1 = 0;
String arrayValueTransfer;

for (int i = 0; i <= temp1Length; i++)
{//loop start; for finding the number of fields and separating them
location1 = temp1.indexOf(charSeparator);
if (location1 >= 0)
{//if true, start; more fields still exist
tempArray1[i] = temp1.substring(0, location1);
temp1 = temp1.substring(location1 + 1);
}//if true, end;
else
{//if false, start; one last field
tempArray1[i] = temp1;
tempArray2Length = i;
i = temp1Length;
}//if false, end;
}//loop end;

String tempArray2[] = new String[tempArray2Length+1];
tempArray1Length = tempArray1.length;
System.out.println(tempArray2Length+1);
for (int i = 0; i <= tempArray2Length; i++)
{//loop start; loop for transfering values into second array and printing them
arrayValueTransfer = tempArray1[i];
tempArray2[i] = arrayValueTransfer;
System.out.println(tempArray2[i]);
}//loop end;

}//end main
}//end class
 

Notes:
This program has to be compiled and run by a Java Compiler, available at http://www.sun.com/ .  Java is case sensitive and therefore the filename must be the exact same case and name as the class name.  If you use Sun's Java SDK for Windows and compile and run from a DOS prompt the command to compile this program is:

javac Separate.java

That will create a compiled file named Separate.class, you then run the command (assuming you have a group of numbers or letters that are each separated by the same character, in our example we will use 2000,2001,2002,2003,miles,comer and then you must include the character that you want it to separate by, in our case a comma, and then put in a space and the character):

java Separate 2000,2001,2002,2003,miles,comer ,

And it will display this text:

6
2000
2001
2002
2003
miles
comer

For these commands to work in this format you must be in the "bin" directory (installed by the SDK), and have javac.exe, java.exe, and Separate.java all in that directory.