Why isnt my code producing 9 17 18 18 1 3 2 2 import javaio

Why isn\'t my code producing

[9, 17, 18, 18]

[1, 3, 2, 2 ]?

import java.io.*;
import java.util.*;

public class PersonalityTestsShell {

public static final int DIMENSIONS = 4;
public static final String set1 =\"1 8 15 22 29 36 43 50 57 64\";
public static final String set2 = \"2 3 9 10 16 17 23 24 30 31 37 38 44 45 51 52 58 59 65 66\";
public static final String set3 = \"4 5 11 12 18 19 25 26 32 33 39 40 46 47 53 54 60 61 67 68\";
public static final String set4= \"6 7 13 14 20 21 27 28 34 35 41 42 48 49 55 56 62 63 69 70\";

public static void main(String[] args){
String answers = \"BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA\";
int[] aCount = new int[4];
int[] bCount = new int[4];
  
System.out.println(answers.length());
count(answers, aCount, bCount);
System.out.println(Arrays.toString(aCount));
System.out.println(Arrays.toString(bCount));

  
  
}
public static void count(String answers, int[] aCount, int[] bCount) {
for (int num = 0; num < answers.length(); num++){
if (inTheSet(set1, num) == true){
if (answers.charAt(num) == \'A\'){
aCount[0]++;
} else if (answers.charAt(num) == \'B\'){
bCount[0]++;
}
} else if (inTheSet(set2, num) == true){
if (answers.charAt(num) == \'A\'){
aCount[1]++;
} else if (answers.charAt(num) == \'B\'){
bCount[1]++;
}
} else if (inTheSet(set3, num) == true){
if (answers.charAt(num) == \'A\'){
aCount[2]++;
} else if (answers.charAt(num) == \'B\'){
bCount[2]++;
}
} else if (inTheSet(set4, num) == true){
if (answers.charAt(num) == \'A\'){
aCount[3]++;
} else if (answers.charAt(num) == \'B\'){
bCount[3]++;
}
}
}
}

public static boolean inTheSet(String set, int num) {
String[] setx = set.split(\" \");
int[] test1 = new int[setx.length];
for (int n = 0; n < setx.length; n++){
test1[n] = Integer.parseInt(setx[n]);
}
  
for (int j = 0; j < test1.length; j++){
if (test1[j] == num){
return true;
}
}
return false;
}
}

Solution

// PersonalityTestsShell.java
/*
for loop in the count method ranges from 0 to answers.length() -1, because of which num counter in the for loop
is matched to the sets accordingly and the aCount and bCount array is updated.

To get the desired output, add space in from of the string answers, thus the for loop in the method count
will range from num = 1 to num = answers.length() which will producre the desired result
*/

import java.io.*;
import java.util.*;
public class PersonalityTestsShell
{
public static final int DIMENSIONS = 4;
public static final String set1 =\"1 8 15 22 29 36 43 50 57 64\";
public static final String set2 = \"2 3 9 10 16 17 23 24 30 31 37 38 44 45 51 52 58 59 65 66\";
public static final String set3 = \"4 5 11 12 18 19 25 26 32 33 39 40 46 47 53 54 60 61 67 68\";
public static final String set4= \"6 7 13 14 20 21 27 28 34 35 41 42 48 49 55 56 62 63 69 70\";
public static void main(String[] args)
{
String answers = \" \";
answers = answers + \"BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA\";

int[] aCount = new int[4];
int[] bCount = new int[4];
  
System.out.println(answers.length());
count(answers, aCount, bCount);
System.out.println(Arrays.toString(aCount));
System.out.println(Arrays.toString(bCount));
  
  
}
public static void count(String answers, int[] aCount, int[] bCount)
{
for (int num = 1; num <= answers.length(); num++)
{
if (inTheSet(set1, num) == true)
{
if (answers.charAt(num) == \'A\')
{
aCount[0]++;
}

else if (answers.charAt(num) == \'B\')
{
bCount[0]++;
}
}

else if (inTheSet(set2, num) == true)
{
if (answers.charAt(num) == \'A\')
{
aCount[1]++;
}

else if (answers.charAt(num) == \'B\')
{
bCount[1]++;
}
}

else if (inTheSet(set3, num) == true)
{
if (answers.charAt(num) == \'A\')
{
aCount[2]++;
}

else if (answers.charAt(num) == \'B\')
{
bCount[2]++;
}
}

else if (inTheSet(set4, num) == true)
{
if (answers.charAt(num) == \'A\')
{
aCount[3]++;
}

else if (answers.charAt(num) == \'B\')
{
bCount[3]++;
}
}
}
}
public static boolean inTheSet(String set, int num)
{
String[] setx = set.split(\" \");
int[] test1 = new int[setx.length];
for (int n = 0; n < setx.length; n++)
{
test1[n] = Integer.parseInt(setx[n]);
}
  
for (int j = 0; j < test1.length; j++)
{
if (test1[j] == num)
{
return true;
}
}
return false;
}
}

/*
output:

71
[1, 17, 18, 18]
[9, 3, 2, 2]


*/

Why isn\'t my code producing [9, 17, 18, 18] [1, 3, 2, 2 ]? import java.io.*; import java.util.*; public class PersonalityTestsShell { public static final int D
Why isn\'t my code producing [9, 17, 18, 18] [1, 3, 2, 2 ]? import java.io.*; import java.util.*; public class PersonalityTestsShell { public static final int D
Why isn\'t my code producing [9, 17, 18, 18] [1, 3, 2, 2 ]? import java.io.*; import java.util.*; public class PersonalityTestsShell { public static final int D

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site