You are on page 1of 2

//Francis Wilson, CSE 142, Section AG, Assignment #7

//5.27.14
//This program reads a file containing personality
//test answers and returns the number of a and b
//answers, the percentage of b answers, and the
//diagnostic personality type.
import java.util.*;
import java.io.*;
public class PersonalityTest {
//Represents how many letters the final
//output will have.
public static final int DIMENSION = 4;

public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
header();
String userInput = console.next();
System.out.println("");
Scanner inputFile = new Scanner(new File(userInput));
while(inputFile.hasNextLine()) {
String name = inputFile.nextLine();
String results = inputFile.nextLine();
int[] aCount = counter(results, 'a' , 'A');
int[] bCount = counter(results, 'b', 'B');
System.out.println(name + ":");
comparison(aCount, bCount);
percentageCalculation(aCount, bCount);
}
}
//Prints the query for the file name.
public static void header(){
System.out.println("This program calculates the personality");
System.out.println("types in the selected file. I'm");
System.out.println("an ENTJ!");
System.out.println("");
System.out.print("Input file name: ");
}
//Takes a line from the file, as well as the two letters
//representing the answers, and checks every character
//in the line, keeping count of a and b answers.
public static int[] counter(String results, char inputLower, char inputUpper)
{
int[] tally = new int[DIMENSION];

for(int i = 0; i < results.length()/7; i++) {
for (int count = 1; count <= 7; count++) {
char c = results.charAt((7 * i) + count - 1);
if((c == inputUpper || c == inputLower)) {
tally[count/2] = tally[count/2] + 1;
}
}
}
return tally;
}

//This method compares and prints an array containing
//the number of a and b answers.
public static void comparison (int[] aCount, int[] bCount) {
String[] answers = new String[DIMENSION];
for(int i = 0; i < DIMENSION; i++) {
answers[i] = (aCount[i] +"A-" + bCount[i] + "B");
}
System.out.println("answers: " + Arrays.toString(answers));
}
//This method calculates the percentage of b answers,
//and returns a personality type based on the percentages.
public static void percentageCalculation(int[] aCount, int[] bCount) {
int[] percentageB = new int[DIMENSION];
for(int i = 0; i < DIMENSION; i++) {
double d = ((1.0*bCount[i]/(aCount[i] + bCount[i])) * 100);
percentageB[i] = (int)Math.round(d);
}
System.out.println("percent B: " + Arrays.toString(percentageB));
System.out.print("type: ");
for(int i = 0; i < DIMENSION; i++) {
if (percentageB[i] == 50) {
System.out.print("X");
} else if (percentageB[i] > 50 && i == 0) {
System.out.print("I");
} else if (i == 0) {
System.out.print("E");
} else if (percentageB[i] > 50 && i == 1) {
System.out.print("N");
} else if (i == 1) {
System.out.print("S");
} else if (percentageB[i] > 50 && i == 2) {
System.out.print("F");
} else if (i == 2) {
System.out.print("T");
} else if (percentageB[i] > 50 && i == 3) {
System.out.print("P");
} else if (i == 3) {
System.out.print("J");
}
}
System.out.println("");
System.out.println("");
}
}

You might also like