Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class PersonalityTest1 {
- public static final int DIMENSIONS = 4; //the number of personality dimensions/categories
- public static void main(String[] args) throws FileNotFoundException{
- fileProcess();
- }
- public static void fileProcess() throws FileNotFoundException {
- Scanner console = new Scanner(System.in);//create scanner for user input
- File fileName = inputFile(console); //store the file the user input typed in
- Scanner read1 = new Scanner(fileName);//create scanner for the .txt file specified by user
- Scanner inputFile = new Scanner(fileName); //make a new scanner to read the file now that we know it exists
- while (inputFile.hasNextLine()) { // while the file has text in the line read through it
- String name = inputFile.nextLine(); // first line is the name of the person who took the test, go to the next line
- String answers = inputFile.nextLine().toUpperCase(); // line after the name is the
- int[] a = counterA(answers); // declares all the methods to the parameters
- int[] b = counterB(answers);
- int[] percentage = percentage(a, b);
- String[] type = type(percentage);
- output(name, percentage, type, a, b, inputFile);
- }
- }
- public static File inputFile(Scanner console) {//ask for file
- System.out.print("Input file name: ");
- File answer = new File(console.nextLine());
- while (!answer.exists()) { //if it doesn't exist, throw an error
- System.out.println("File not found. Try again: ");
- answer = new File(console.nextLine()); //store the file the user input typed in
- }
- return answer;
- }
- // Method to count B's
- public static int[] counterB(String answers) { // Takes from user input
- int[] counterB = new int[DIMENSIONS];
- for (int i = 0; i < answers.length(); i++) {
- if(answers.charAt(i) == 'B'){
- if((i+1) % 7 == 1){ // based off of multiples of 7
- counterB[0]++;
- }
- else if((i+1) % 7 == 2 || (i+1) % 7 == 3){
- counterB[1]++;
- }
- else if((i+1) % 7 == 4 || (i+1) % 7 == 5){
- counterB[2]++;
- }
- else if((i+1) % 7 == 6 || (i+1) % 7 == 0){
- counterB[3]++;
- }
- }
- }
- return counterB;
- }
- // method counts the A's
- public static int[] counterA(String answers) {
- int[] counterA = new int[DIMENSIONS];
- for (int i = 0; i < answers.length(); i++) {
- if(answers.charAt(i) == 'A'){
- if((i+1) % 7 == 1){ // based off of multiples of 7
- counterA[0]++;
- }
- else if((i+1) % 7 == 2 || (i+1) % 7 == 3){
- counterA[1]++;
- }
- else if((i+1) % 7 == 4 || (i+1) % 7 == 5){
- counterA[2]++;
- }
- else if((i+1) % 7 == 6 || (i+1) % 7 == 0){
- counterA[3]++;
- }
- }
- }
- return counterA;
- }
- //method to take the percentage of B for each type section and see what category they fit in
- public static String[]type(int[] percentage) { // string type takes the int array from the percentage method
- String[] type = new String[DIMENSIONS]; // type is a string with a array length of 4
- for (int i = 0; i < DIMENSIONS; i++) {
- if(i == 0){
- if(percentage[i] > 50){
- type[i] = "I";
- }
- else if (percentage[i] < 50){
- type[i] = "E";
- }
- else {
- type[i] = "X";
- }
- }
- if(i == 1){
- if(percentage[i] > 50){
- type[i] = "N";
- }
- else if (percentage[i] < 50){
- type[i] = "S";
- }
- else {
- type[i] = "X";
- }
- }
- if(i == 2){
- if(percentage[i] > 50){
- type[i] = "F";
- }
- else if (percentage[i] < 50){
- type[i] = "T";
- }
- else {
- type[i] = "X";
- }
- }
- if(i == 3){
- if(percentage[i] > 50){
- type[i] = "P";
- }
- else if (percentage[i] < 50){
- type[i] = "J";
- }
- else {
- type[i] = "X";
- }
- }
- }
- return type;
- }
- // method to calculate the percentages of 'B' answers
- public static int[] percentage(int[] counterA, int[] counterB) { // takes the information from the method count B
- int[] percentage = new int[DIMENSIONS];
- for(int i = 0; i < counterB.length; i++){
- double d = (double) counterB[i] / (counterA[i] + counterB[i]) * 100;
- percentage[i] = (int) Math.round(d);
- }
- return percentage;
- }
- // Output
- public static void output(String name, int[] percentage, String[] type, int[] counterA, int[] counterB, Scanner inputFile) {
- System.out.println(name + ":");
- System.out.print(" ");
- for(int i = 0; i < DIMENSIONS; i++){
- if(i == 3){
- System.out.print(counterA[i] + "A-" + counterB[i] + "B");
- }
- else{
- System.out.print(counterA[i] + "A-" + counterB[i] + "B ");
- }
- }
- System.out.println();
- System.out.print(" ");
- for(int i = 0; i < DIMENSIONS; i++){
- if(i == 0){
- System.out.print("[" + percentage[i] + "%, ");
- }
- else if(i == 3){
- System.out.print(percentage[i] + "%]");
- }
- else {
- System.out.print(percentage[i] + "%, ");
- }
- }
- System.out.print(" = ");
- for(int i = 0; i < DIMENSIONS; i++){
- System.out.print(type[i]);
- }
- System.out.println();
- if(inputFile.hasNextLine()){
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement