HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Using contents of an integer array to print a String

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraycontentsprintusingstringinteger

Problem

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();

Solution

Sorry it's getting late now, still I want to give you my first suggestions.

-
Try to choose more meaningful names for variables (including constants). Example: CONSTANT -> NUMBER_OF_PERSONALITY_TYPES

-
I generalized your computePersonality function (untested, care!).

-
Some line breaks always improve readability in my opinion.

Next steps:

-
Next I would extract the block of code inside the while loop within the processFile method. This is now the longest method, maybe we can break it down some more.

-
Extract the remaining constants (at least the number 7 as far as I have seen) and give it meaningful names.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;

public class Personality {

    public static final int NUMBER_OF_PERSONALITY_TYPES = 4;

    // this is not an optimal name
    public static final int PERCENT_B_MIDDLE = 50;

    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);
        console.close();

    }

    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[NUMBER_OF_PERSONALITY_TYPES];
            computeScores(data, totalA, totalB);
            int[] countsA = new int[NUMBER_OF_PERSONALITY_TYPES];
            int[] countsB = new int[NUMBER_OF_PERSONALITY_TYPES];
            int[] percentB = new int[NUMBER_OF_PERSONALITY_TYPES];
            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  PERCENT_B_MIDDLE) {

            return letterTwo;

        } else {

            return letterThree;
        }
    }

    public static void print(PrintStream output, String name, int[] percentB, String personality) {

        String percent = Arrays.toString(percentB);
        output.println(name + ": " + percent + " = " + personality);
    }
}

Code Snippets

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;

public class Personality {

    public static final int NUMBER_OF_PERSONALITY_TYPES = 4;

    // this is not an optimal name
    public static final int PERCENT_B_MIDDLE = 50;

    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);
        console.close();

    }

    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[NUMBER_OF_PERSONALITY_TYPES];
            computeScores(data, totalA, totalB);
            int[] countsA = new int[NUMBER_OF_PERSONALITY_TYPES];
            int[] countsB = new int[NUMBER_OF_PERSONALITY_TYPES];
            int[] percentB = new int[NUMBER_OF_PERSONALITY_TYPES];
            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 < NUMBER_OF_PERSONALITY_TYPES; i++) {

                countsA[i] = totalA[j] + totalA[j - 1];
                countsB[i] = totalB[j] + tota

Context

StackExchange Code Review Q#111623, answer score: 4

Revisions (0)

No revisions yet.