111 A8
Code
import java.util.Scanner;
public class YuepA08
{
public static void main (String[] args)
{
String mutation1, mutatione, mA, mB, mC, mD, mX, mY, mZ, fileName, ext, dext;
Scanner keybd = new Scanner(System.in);
System.out.print("Please enter a filename to convert: ");
//asks for input(x) and trims it(t)
String x = keybd.nextLine();
String t = x.trim();
int length = t.length();
//check length of t if 0 then error
if ( length == 0 ){
System.out.println("Error: Didn't enter a file name to convert.");
return;
}
//check first char, if '.' then error
else if ( length >= 1) {
char firstChar = t.charAt(0);
if (firstChar == '.') {
System.out.println("File can't start with '.' ");
return;
}
}
System.out.println("File Name: " + t);
int dot = t.lastIndexOf('.');
//if no dot, print t
if (dot < 0) {
fileName = t;
ext = "";
dext = "";
System.out.println("Name only: " + fileName);
System.out.println("Extension: " + ext);
}
//if dot, print filename(b4 dot) & ext(after dot)
else {
fileName = t.substring(0, dot);
ext = t.substring(dot + 1);
dext = t.substring(dot);
System.out.println("Name only: " + fileName);
System.out.println("Extension: " + ext);
}
//name manipulations--------------------
mutation1 = fileName.replace(' ', '_');
int m1Length = mutation1.length();
if (m1Length > 8) {
mA = mutation1.substring(0, 8);
}
else {
mA = mutation1;
}
mB = mA.replace('.', '_');
mC = mB.toUpperCase();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//extension manipulations
mutatione = dext.replace(' ', '_');
int meLength = mutatione.length();
if (meLength > 4) {
mX = mutatione.substring(0, 4);
}
else {
mX = mutatione;
}
mY = mX.toUpperCase();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
System.out.println("8.3 Filename: " + mC + mY);
}
}