onetwenty8k
2[H]4U
- Joined
- Nov 24, 2006
- Messages
- 2,554
Here is the completed code. I realize there are some useless things in there but whatever.
Code:
import java.io.*;
import java.util.*;
public class PersonalityScorer {
public static void main(String[] args)
throws IOException {
readInput(); //Main calls one method
}
public static String InputMethod() //Asks for file name
throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("Input file name: ");
String inputFile = console.next();
Scanner input = new Scanner(new File(inputFile));
boolean exists = (new File(inputFile)).exists();
return inputFile;
}
//Method gathers all information to output to file
public static void OutputMethod(int[] aCounts, int[] bCounts, String name, PrintStream output)
throws FileNotFoundException {
String finalOutput = ""; //Initializes all the variables
String compOne = "";
String compTwo = "";
String compThree = "";
String compFour = "";
String answerOutput = aCounts[0] + "A-" + bCounts[0] + "B " + aCounts[1] + "A-" + bCounts[1] + "B " + aCounts[2] + "A-" + bCounts[2] + "B " + aCounts[3] + "A-" + bCounts[3] + "B ";
if (aCounts[0] < bCounts[0]) { //Finds correct letters for ratio between A and B
compOne = "I";
} else if (aCounts[0] > bCounts[0]) {
compOne = "E";
} else {
compOne = "X";
}
if (aCounts[1] < bCounts[1]) {
compTwo = "N";
} else if (aCounts[1] > bCounts[1]) {
compTwo = "S";
} else {
compTwo = "X";
}
if (aCounts[2] < bCounts[2]) {
compThree = "F";
} else if (aCounts[2] > bCounts[2]) {
compThree = "T";
} else {
compThree = "X";
}
if (aCounts[3] < bCounts[3]) {
compFour = "P";
} else if (aCounts[3] > bCounts[3]) {
compFour = "J";
} else {
compFour = "X";
}
//Makes a string to output
finalOutput = finalOutput + name + "\n" + answerOutput + "= " + compOne + compTwo + compThree + compFour;
output.println(finalOutput); //Outputs finalOutput to text file
}
public static void readInput()
throws IOException {
String inputFile = InputMethod();
Scanner input = new Scanner(new File(inputFile));
Scanner console = new Scanner(System.in); //Gathers output file name
System.out.println("Output file name: ");
String output = console.next();
FileOutputStream outputName;
PrintStream file;
outputName = new FileOutputStream (output);
file = new PrintStream (output);
while (input.hasNextLine()) {
String name = input.nextLine();
String answer = input.nextLine();
char[] letters = answer.toCharArray(); //Converts string to array
int[] aCounts = new int[4]; //Creates two arrays to count A and B
int[] bCounts = new int[4];
countAnswers(letters, aCounts, bCounts);
OutputMethod(aCounts, bCounts, name, file); //Outputs info to OutputMethod with parameters
}
}
//Takes the answers array and gathers amount of As and Bs for each component
public static void countAnswers(char[] letters, int[] aCounts, int[] bCounts) {
for (int i = 0; i < letters.length; i++) {
if (i % 7 == 0) {
if (letters[i] == 'A') {
aCounts[0]++;
} if (letters[i] == 'B') {
bCounts[0]++;
} if (letters[i] == '-') {
aCounts[0] = aCounts[0] + 0;
}
}
if (i % 7 == 1 || i % 7 == 2) {
if (letters[i] == 'A') {
aCounts[1]++;
} if (letters[i] == 'B') {
bCounts[1]++;
} if (letters[i] == '-') {
aCounts[1] = aCounts[1] + 0;
}
}
if (i % 7 == 3 || i % 7 == 4) {
if (letters[i] == 'A') {
aCounts[2]++;
} if (letters[i] == 'B') {
bCounts[2]++;
} if (letters[i] == '-') {
aCounts[2] = aCounts[2] + 0;
}
}
if (i % 7 == 5 || i % 7 == 6) {
if (letters[i] == 'A') {
aCounts[3]++;
} if (letters[i] == 'B') {
bCounts[3]++;
} if (letters[i] == '-') {
aCounts[3] = aCounts[3] + 0;
} //Method does not need to return because they are arrays
}
}
}
}