# Thread: Good ol' arrays! Help O_O

1. Member
Join Date
Nov 2011
Posts
6
Rep Power
0

## Good ol' arrays! Help O_O

Okay guys... I'm in my only CS class I have to take and I'm almost done and barely keeping my head above the programming water!
So here's my assignment:
Proff. gave us a file to download and input into the java program (got that :)) and in there are names of 'people' who have taken a Meyers-Briggs personality test and their answers (either A, B, or if they left it blank a -). So it comes in format like this:
Name Here: Bababbbaaaa-AAbaabab until 70 questions of the test are completed. This is how the questions roll:
(1) Extrovert versus Introvert (E vs. I): what energizes you
(2) Sensation versus iNtuition (S vs. N): what you focus on
(3) Thinking versus Feeling (T vs. F): how you interpret what you focus on
(4) Judging versus Perceiving (J vs. P): how you approach life

And the set-up for the arrays is like this:
(1) The first one question is an Introvert/Extrovert question (questions 1, 8, 15...).
(2) The next two questions are for Sensing/iNtuition (questions 2 and 3, 9 and 10...).
(3) The next two questions are for Thinking/Feeling (questions 4 and 5, 11 and 12...).
(4) The next two questions are for Judging/Perceiving (questions 6 and 7, 13 and 14...).

AKA:

dimension 1223344 1223344 1223344 1223344 1223344 1223344 1223344 1223344 1223344 1223344
char BABAAAA BAAAAAA ABAAAAB BAAAAAA BAAAABA BAABAAA BABABAA BAAAAAA BAAAAAA BAAAAAA

My job is to take all of that gibberish and put it into an output that shows how many A's and B's comparatively in each category (the 1,2,3 and 4 categories) so the format comes out like this:
Betty Boop:
<-- this is what I'm having the most problems with!
percent B: [90, 15, 10, 10] <-- this is the percentage of B's compared to A's

type: ISTJ <-- with percent of B's, this is their personality.

So.. here's my code. If you guys come up with a fix for my problem, that would be great. I'm SO not asking you all to do the rest of it for me, just to help me get the 'answers' format correct. If you guys need the file reference, I can email it to you.
Here's my code (and if you can get it less redundant, I would also appreciate that because she's gonna dock points for redundancy lol)

Java Code:
```import java.io.*;
import java.util.*;

public class PersonalityTest {

public static void main(String[] args) throws FileNotFoundException {
intro();
System.out.println();
Scanner input = new Scanner(new File("personality.txt"));[COLOR="#00FFFF"]//File input[/COLOR]
while (input.hasNextLine()) { [COLOR="#00FFFF"]//this is to pull out first name[/COLOR]
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
String name = lineScan.next();

while (lineScan.hasNext()) { [COLOR="#00FFFF"]//this is to pull out last name[/COLOR]
String last = lineScan.next();
System.out.println(name + " " + last + ":");[COLOR="#FF0000"]

// this is to start to count out the four separate categories.

//PROBLEMS START HERE!!![/COLOR]
String b = input.nextLine();
int countsa = 0;
int countsb = 0;
int countsA = 0;
int countsB = 0;

int[] counts = new int[7]; [COLOR="#00FFFF"]//breaks up continuous line of
//'answers' into seven: this is when the categories repeat
// (recall they count as 1223344 for the categories.[/COLOR]

counts[1] = counts[2] + counts[1];
counts[3] = counts[4] + counts[3[COLOR="#00FFFF"]];//I'm trying to add the different arrays[/COLOR]
counts[5] = counts[6] + counts[5[COLOR="#00FFFF"]];//so that I get four categories[/COLOR]

for (int j = 0; j < b.length(); j++) {
if (b.charAt(0) == 'a') {
countsa++;
}
if (b.charAt(0) == 'A') {
countsA++;
}
if (b.charAt(0) == 'b') {
countsb++;
}
if (b.charAt(0) == 'B') {
countsB++;
}
}

int countsa1 = 0;
int countsb1 = 0;
int countsA1 = 0;
int countsB1 = 0;
for (int k = 0; k < b.length(); k++) {
if (b.charAt(1) == 'a') {
countsa1++;
}
if (b.charAt(1) == 'A') {
countsA1++;
}
if (b.charAt(1) == 'b') {
countsb1++;
}
if (b.charAt(1) == 'B') {
countsB1++;
}
}
int countsa3 = 0;
int countsb3 = 0;
int countsA3 = 0;
int countsB3 = 0;
for (int k = 0; k < b.length(); k++) {
if (b.charAt(1) == 'a') {
countsa3++;
}
if (b.charAt(1) == 'A') {
countsA3++;
}
if (b.charAt(1) == 'b') {
countsb3++;
}
if (b.charAt(1) == 'B') {
countsB3++;
}
}
int countsa5 = 0;
int countsb5 = 0;
int countsA5 = 0;
int countsB5 = 0;
for (int k = 0; k < b.length(); k++) {
if (b.charAt(1) == 'a') {
countsa5++;
}
if (b.charAt(1) == 'A') {
countsA5++;
}
if (b.charAt(1) == 'b') {
countsb5++;
}
if (b.charAt(1) == 'B') {
countsB5++;
}
}
[COLOR="#00FFFF"]//My println statement to print results in four categories[/COLOR]
System.out.println("answers: [" + (countsa1 + countsA1) + "A - "
+ (countsB1 + countsb1) + "B"
+ ", " + (countsa + countsA) + "A - " + (countsB + countsb)
+ "B, " + (countsa3 + countsA3) + "A - " + (countsB3
+ +countsb3) + "B, " + (countsa5 + countsA5) + "A - "
+ (countsB5 + countsb5) + "B]");

}
}
}

public static void intro() { [COLOR="#00FFFF"]//my program introduction[/COLOR]
System.out.println("The classic Myers-Briggs personality test is a test"
+ " that measures four independent dimensions of your personality:"
+ "\n(1) Extrovert versus Introvert (E vs. I): what energizes you"
+ "\n(2) Sensation versus iNtuition (S vs. N): what you focus on"
+ "\n(3) Thinking versus Feeling (T vs. F): how you interpret what you focus on"
+ "\n(4) Judging versus Perceiving (J vs. P): how you approach life");
System.out.println("There are a total of 70 questions, all can be answered"
+ " with A, B, or - (if there is no answer). The following is"
+ " collected data from a few test takers:");
}
}```
I hope this is readable, I know it's quite a mess. But that's why I'm asking for help
Last edited by Fubarable; 11-16-2011 at 12:21 AM. Reason: code tags added

2. ## Re: Good ol' arrays! Help O_O

No it is not readable. Do not use coloured text. When posting code use code tags. Place [ code ] before and [ /code ] (without the spaces) after your code. If you get any errors post them. Ask a specific question. Do not embed the question in the code, post it before or after.

3. ## Re: Good ol' arrays! Help O_O

I have edited your post and have added [code] and [/code] above and below your post. I second all that Junky had to say. Please try to formulate as specific a set of questions as possible, otherwise we'll have little choice but to simply refer you to the tutorials.

4. ## Re: Good ol' arrays! Help O_O

Why is there a winking smilie in the middle of my post?

5. ## Re: Good ol' arrays! Help O_O

Originally Posted by Junky
Why is there a winking smilie in the middle of my post?
Perhaps you've gone soft on us?

6. ## Re: Good ol' arrays! Help O_O

Originally Posted by Junky
Why is there a winking smilie in the middle of my post?
Stoopid browser not refreshing properly!

7. Member
Join Date
Nov 2011
Posts
6
Rep Power
0

## Re: Good ol' arrays! Help O_O

Okay.. so any ideas? Thanks for editing it btw, but do we have a code fixer or no?

8. ## Re: Good ol' arrays! Help O_O

I have plenty of ideas. If you would like a specific one then ask a specific question.

9. Member
Join Date
Nov 2011
Posts
6
Rep Power
0

## Re: Good ol' arrays! Help O_O

Okay so you all need a more specific question... So I said up there the format of the file I'm importing and how I have to use the 'answers' to output it in a different way. each string of seven 'answers' Ex: ABAAbab is a 'set' and will restart with the different type of question (an extrovert/introvert) in an array patter such as this: 1223344. There are seventy questions so there are 10 sets of these tiny sets. I need to add ALL of the 1's (a's and b's seperately) ALL of the 2's (a's and b's separately) and so on to be able to compare the a answers and b answers of the different types of questions throughout the whole questionaire. It's confusing but not really... just read it a couple times.
I'm just having problems getting the arrray set up so that it makes groups of seven and loops through the groups of seven to count all of the similar items (all the 1's, 2's etc.).
Thanks

10. ## Re: Good ol' arrays! Help O_O

Why do you need an array? Just use a loop to and substring to get a block of seven chars at a time. Then you can read those seven chars individually and update some variables. At the end of all the reading you have some math to perform which is exactly the same in any programming language as well as on paper.

11. Member
Join Date
Nov 2011
Posts
6
Rep Power
0

## Re: Good ol' arrays! Help O_O

Well, that was my 'intention' with the mess of for loops up there which obviously didn't work but I think should, there's probably just a curly bracket in the wrong place. I'm actually not even sure why it's not counting right... it's counting that EVERYTHING is a B or an A, which is odd... but I'm still using the arrays up there. I guess I'm not too sure what you're getting at by making a block w/o the arrays to make such a block.

12. ## Re: Good ol' arrays! Help O_O

Java Code:
```public class Test {
public static void main(String[] args) {
String text = "ABBBAAABABABABAAABBBB";
int start = 0;
int aCount = 0;
int bCount = 0;
while(start < text.length()) {
String subset = text.substring(start, start + 7);
System.out.println(subset);
start += 7;
if(subset.charAt(0) == 'A') {
aCount++;
} else {
bCount++;
}
}
System.out.println(aCount);
System.out.println(bCount);
}
}```
A rough idea what needs to be done. You should be able to build on it.

13. Member
Join Date
Nov 2011
Posts
6
Rep Power
0

## Re: Good ol' arrays! Help O_O

So it's kindof working and I get the idea, but it wont let me 'scan' the line and get info from the line, it only allows the input at the String text = "blahblahblah";... I tried putting in another scanner but it doesn't come out with any results.

14. Member
Join Date
Nov 2011
Posts
6
Rep Power
0

## Re: Good ol' arrays! Help O_O

HAHA! Success! Oh wow, thanks so much! I hope you feel good helping noobs, because you're great at it lol!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•