/**          Program to read in a string assumed to be a filename
 *           of form name.suffix ans split it into "name" and "suffix"
 */

import uk.ac.ed.ph.sciprog.*;

public class FileSplit {
    public static void main(String args[]) {
	
	//                  Set up panel to read a String

	Display panel = new Display("Reading filenames");
	Input fileInput = new Input("Give a Filename","");
	panel.addInput(fileInput);

	//                  Loop reading inputs

	while(true) {
	    panel.waitForButtonPress();
	    
	    //              Read in the String

	    String fileName = fileInput.getString();
	    panel.println("Filename is `" + fileName + "'");

	    //             Find the location of the Last `.'

	    int dotPosition = fileName.lastIndexOf('.');
	 
	    if (dotPosition < 0) {           // There is no dot
		panel.println("Not in `name.suffix' format");
	    }
	    else {

		//    Extarct name, being from start to the dot 
		String name = fileName.substring(0,dotPosition);
		
		//    Extract suffix, being from after dot to end        
		String suffix = fileName.substring(dotPosition + 1, 
						   fileName.length());
		
		panel.println("Root name is `" + name + "'");
		panel.println("Suffix is `" + suffix + "'");
	    }

	}
    }
}
