I have written this code that takes input from a file and creates an output file that reports each person's name, percentage of B
answers, and personality. However, the my computePersonality
method, which takes the contents of an integer array and converts it into a string of \$4\$ letters based on if the number in the array is greater than, equal to, or less than \$50\$, is pretty long.
I'm worried about the length of the method and the redundancy? Also, any comments on the efficiency of the overall code would be appreciated.
Here's an example of an input file:
Betty Boop
BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA
Snoopy
AABBAABBBBBABABAAAAABABBAABBAAAABBBAAABAABAABABAAAABAABBBBAAABBAABABBB
Bugs Bunny
aabaabbabbbaaaabaaaabaaaaababbbaabaaaabaabbbbabaaaabaabaaaaaabbaaaaabb
Daffy Duck
BAAAAA-BAAAABABAAAAAABA-AAAABABAAAABAABAA-BAAABAABAAAAAABA-BAAABA-BAAA
My main concern is the computePersonality
. Here's the complete class:
import java.util.*;
import java.io.*;
public class Personality {
public static final int CONSTANT = 4;
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
giveIntro();
System.out.print("input file name? ");
String inputFile = console.next();
System.out.print("output file name? ");
String outputFile = console.next();
Scanner input = new Scanner(new File(inputFile));
PrintStream output = new PrintStream(new File(outputFile));
processFile(input, output);
}
public static void giveIntro() {
System.out.println("This program processes a file of answers to the");
System.out.println("Keirsey Temperament Sorter. It converts the");
System.out.println("various A and B answers for each person into");
System.out.println("a sequence of B-percentages and then into a");
System.out.println("four-letter personality type.");
System.out.println();
}
public static void processFile(Scanner input, PrintStream output) {
while (input.hasNextLine()) {
String name = input.nextLine();
String data = input.nextLine();
int[] totalA = new int[7];
int[] totalB = new int[7];
int[] totalAB = new int[CONSTANT];
computeScores(data, totalA, totalB);
int[] countsA = new int[CONSTANT];
int[] countsB = new int[CONSTANT];
int[] percentB = new int[CONSTANT];
countsOfAB(totalA, totalB, countsA, countsB, totalAB);
computePercentB(countsB, totalAB, percentB);
String personality = computePersonality(percentB);
print(output, name, percentB, personality);
}
}
public static void computeScores(String data, int[] totalA, int[] totalB) {
for (int i = 0; i < 7; i++) {
for (int j = i; j < data.length(); j += 7) {
char c = data.charAt(j);
if (c == 'a' || c == 'A') {
totalA[i]++;
} else if (c == 'b' || c == 'B') {
totalB[i]++;
}
}
}
}
public static void countsOfAB(int[] totalA, int[] totalB, int[] countsA, int[] countsB, int[] totalAB) {
countsA[0] = totalA[0];
countsB[0] = totalB[0];
totalAB[0] = totalA[0] + totalB[0];
for (int j = 2; j < 7; j += 2) {
for (int i = j / 2; i < CONSTANT; i++) {
countsA[i] = totalA[j] + totalA[j - 1];
countsB[i] = totalB[j] + totalB[j - 1];
totalAB[i] = countsA[i] + countsB[i];
}
}
}
public static void computePercentB(int[] countsB, int[] totalAB, int[] percentB) {
for (int i = 0; i < CONSTANT; i++) {
percentB[i] = (int) Math.round(countsB[i] / (double) totalAB[i] * 100) ;
}
}
public static String computePersonality(int[] percentB) {
String personality = "";
if (percentB[0] < 50) {
personality += "E";
} else if (percentB[0] > 50) {
personality += "I";
} else {
personality += "X";
}
if (percentB[1] < 50) {
personality += "S";
} else if (percentB[1] > 50) {
personality += "N";
} else {
personality += "X";
}
if (percentB[2] < 50) {
personality += "T";
} else if (percentB[2] > 50) {
personality += "F";
} else {
personality += "X";
}
if (percentB[3] < 50) {
personality += "J";
} else if (percentB[3] > 50) {
personality += "P";
} else {
personality += "X";
}
return personality;
}
public static void print(PrintStream output, String name, int[] percentB, String personality) {
String percent = Arrays.toString(percentB);
output.println(name + ": " + percent + " = " + personality);
}
}