• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Annoying java problem

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
            }
        }
    }
}
 
To increment index 0 of aCounts you should be able to do:

aCounts[0]++;

or

aCounts[0] = aCounts[0] + 1;
 
Well that worked but there seems to be a logic error in my code. Check the first post for the edited code.
 
Are you familiar with modular arithmetic? You could clean up your if conditions a lot if you simply used division modulo 7 and used a switch statement.

Code:
for (int i = 0; i < letters.length; i++) {
            int remainder = i % 7;
            switch (remainder) {
            case 0: 
                if (letters[i] == 'A') {
                    aCounts[0]++;
                } else if (letters[i] == 'B') {
                    bCounts[0]++;
                }
            break;
            case 1:
            case 2: 
                if (letters[i] == 'A') {
                    aCounts[1]++;
                } else if (letters[i] == 'B') {
                    bCounts[1]++;
                }
                break;
            case 3:
            case 4: 
                if (letters[i] == 'A') {
                    aCounts[2]++;
                } else if (letters[i] == 'B') {
                    bCounts[2]++;
                break;
            case 5:
            case 6: 
                if (letters[i] == 'A') {
                    aCounts[3]++;
                } else if (letters[i] == 'B') {
                    bCounts[3]++;
                }
                break;
            }
        }
 
Well I was trying to clean it up but I wasn't sure what to put. We haven't learned switch statements yet :(
 
Rofl, bad error in addition...that was my problem with outputs. I did it without switch and did modular arithmetic. Check first post.
 
One last question. Can someone explain how to use PrintStream. I'm a little confused while looking at the API.
 
I managed to fix the printstream problem by creating it before the loop and then passing it in as a parameter.
 
I don't understand, why not just initialize it to an empty string ("") or do a check where ever you may first encounter it (if string is null, initialize it to an empty string)...?
 
I forgot to say that I fixed it by creating the PrintStream object first and then passing it in as a parameter. That way, every time the method was called, it didn't overwrite.
 
A couple things I noticed while looking at that block of code:

Have you learned about closing streams yet? You should get into the habit of cleaning up those streams by closing them after you are done using them.

Code:
stream.open();
//do stuff
stream.close();

If you've learned about finally you can do the following:

Code:
try
{
    stream.open;
    //do stuff
}
finally
{
    if(stream != null)  stream.close();
}

What's the point of the following? You can probably remove it...
Code:
if (letters[i] == '-') {
                    aCounts[0] = aCounts[0] + 0;
                }

Take a look at this block again, something is definately wrong here (hint: values you'll output from aCounts and bCounts):
Code:
        while (input.hasNextLine()) {
            String name = input.nextLine();
            String answer = input.nextLine();
            char[] letters = answer.toCharArray();
            int[] aCounts = new int[4];
            int[] bCounts = new int[4];
            countAnswers(letters, aCounts, bCounts);
            OutputMethod(aCounts, bCounts, name);
        }

Get in the habit of using symbolic constants in place of numeric literals:
In the following block, we dont know what 4 stands for. Use a constant in place of it.
Code:
   int[] aCounts = new int[4];
   int[] bCounts = new int[4];
 
The second to last thing you were talking about, it's going to output 0, 0, 0, 0 but when it is returned by the method, it will update the array. We haven't learned closing streams or finally yet but I don't think the prof will really care. The one thing that we're not allowed to do yet is handle exceptions (got 3 points taken off last HW cause I caught an exception). Part of it was written exceptionally bad (get it?).
 
Back
Top