Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class PersonalityTest1 {
  5. public static final int DIMENSIONS = 4; //the number of personality dimensions/categories
  6.  
  7. public static void main(String[] args) throws FileNotFoundException{
  8.  
  9. fileProcess();
  10.  
  11. }
  12. public static void fileProcess() throws FileNotFoundException {
  13. Scanner console = new Scanner(System.in);//create scanner for user input
  14. File fileName = inputFile(console); //store the file the user input typed in
  15. Scanner read1 = new Scanner(fileName);//create scanner for the .txt file specified by user
  16. Scanner inputFile = new Scanner(fileName); //make a new scanner to read the file now that we know it exists
  17. while (inputFile.hasNextLine()) { // while the file has text in the line read through it
  18. String name = inputFile.nextLine(); // first line is the name of the person who took the test, go to the next line
  19. String answers = inputFile.nextLine().toUpperCase(); // line after the name is the
  20. int[] a = counterA(answers); // declares all the methods to the parameters
  21. int[] b = counterB(answers);
  22. int[] percentage = percentage(a, b);
  23. String[] type = type(percentage);
  24. output(name, percentage, type, a, b, inputFile);
  25. }
  26.  
  27. }
  28. public static File inputFile(Scanner console) {//ask for file
  29. System.out.print("Input file name: ");
  30. File answer = new File(console.nextLine());
  31. while (!answer.exists()) { //if it doesn't exist, throw an error
  32. System.out.println("File not found. Try again: ");
  33. answer = new File(console.nextLine()); //store the file the user input typed in
  34. }
  35. return answer;
  36. }
  37.  
  38.  
  39. // Method to count B's
  40.  
  41.  
  42. public static int[] counterB(String answers) { // Takes from user input
  43. int[] counterB = new int[DIMENSIONS];
  44. for (int i = 0; i < answers.length(); i++) {
  45. if(answers.charAt(i) == 'B'){
  46. if((i+1) % 7 == 1){ // based off of multiples of 7
  47. counterB[0]++;
  48. }
  49. else if((i+1) % 7 == 2 || (i+1) % 7 == 3){
  50. counterB[1]++;
  51. }
  52. else if((i+1) % 7 == 4 || (i+1) % 7 == 5){
  53. counterB[2]++;
  54. }
  55. else if((i+1) % 7 == 6 || (i+1) % 7 == 0){
  56. counterB[3]++;
  57. }
  58. }
  59. }
  60. return counterB;
  61. }
  62. // method counts the A's
  63. public static int[] counterA(String answers) {
  64. int[] counterA = new int[DIMENSIONS];
  65. for (int i = 0; i < answers.length(); i++) {
  66. if(answers.charAt(i) == 'A'){
  67. if((i+1) % 7 == 1){ // based off of multiples of 7
  68. counterA[0]++;
  69. }
  70. else if((i+1) % 7 == 2 || (i+1) % 7 == 3){
  71. counterA[1]++;
  72. }
  73. else if((i+1) % 7 == 4 || (i+1) % 7 == 5){
  74. counterA[2]++;
  75. }
  76. else if((i+1) % 7 == 6 || (i+1) % 7 == 0){
  77. counterA[3]++;
  78. }
  79. }
  80. }
  81. return counterA;
  82. }
  83. //method to take the percentage of B for each type section and see what category they fit in
  84. public static String[]type(int[] percentage) { // string type takes the int array from the percentage method
  85. String[] type = new String[DIMENSIONS]; // type is a string with a array length of 4
  86. for (int i = 0; i < DIMENSIONS; i++) {
  87. if(i == 0){
  88. if(percentage[i] > 50){
  89. type[i] = "I";
  90. }
  91. else if (percentage[i] < 50){
  92. type[i] = "E";
  93. }
  94. else {
  95. type[i] = "X";
  96. }
  97. }
  98. if(i == 1){
  99. if(percentage[i] > 50){
  100. type[i] = "N";
  101. }
  102. else if (percentage[i] < 50){
  103. type[i] = "S";
  104. }
  105. else {
  106. type[i] = "X";
  107. }
  108. }
  109. if(i == 2){
  110. if(percentage[i] > 50){
  111. type[i] = "F";
  112. }
  113. else if (percentage[i] < 50){
  114. type[i] = "T";
  115. }
  116. else {
  117. type[i] = "X";
  118. }
  119. }
  120. if(i == 3){
  121. if(percentage[i] > 50){
  122. type[i] = "P";
  123. }
  124. else if (percentage[i] < 50){
  125. type[i] = "J";
  126. }
  127. else {
  128. type[i] = "X";
  129. }
  130. }
  131. }
  132. return type;
  133. }
  134. // method to calculate the percentages of 'B' answers
  135. public static int[] percentage(int[] counterA, int[] counterB) { // takes the information from the method count B
  136. int[] percentage = new int[DIMENSIONS];
  137. for(int i = 0; i < counterB.length; i++){
  138. double d = (double) counterB[i] / (counterA[i] + counterB[i]) * 100;
  139. percentage[i] = (int) Math.round(d);
  140.  
  141. }
  142. return percentage;
  143. }
  144.  
  145.  
  146. // Output
  147.  
  148.  
  149. public static void output(String name, int[] percentage, String[] type, int[] counterA, int[] counterB, Scanner inputFile) {
  150. System.out.println(name + ":");
  151. System.out.print(" ");
  152. for(int i = 0; i < DIMENSIONS; i++){
  153. if(i == 3){
  154. System.out.print(counterA[i] + "A-" + counterB[i] + "B");
  155. }
  156. else{
  157. System.out.print(counterA[i] + "A-" + counterB[i] + "B ");
  158. }
  159. }
  160. System.out.println();
  161. System.out.print(" ");
  162. for(int i = 0; i < DIMENSIONS; i++){
  163. if(i == 0){
  164. System.out.print("[" + percentage[i] + "%, ");
  165. }
  166. else if(i == 3){
  167. System.out.print(percentage[i] + "%]");
  168. }
  169. else {
  170. System.out.print(percentage[i] + "%, ");
  171. }
  172. }
  173. System.out.print(" = ");
  174. for(int i = 0; i < DIMENSIONS; i++){
  175. System.out.print(type[i]);
  176. }
  177. System.out.println();
  178. if(inputFile.hasNextLine()){
  179. System.out.println();
  180. }
  181. }
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement