0

So, I'm new to programming and I have this exercise where I have to read an int[][] array with the age and a handicap level of people trying to start a membership in a Club, with two categories, Senior and Open. My job is to read the array for example [[45, 12],[55,21],[19, -2]] where the first int is the age and the second the handicap level. If the age is at least 55 and the handicap level is higher than 7 then the person gets a Senior membership if not he gets an Open one. My idea was to see the int[][] as a matrix and add both numbers (age and level) and if the number is higher than 62 I would classify it as a Senior otherwise as open. My method looks like this:

public class montecarlo {
    static String[] openOrSenior(int[][] a) {
        int i, j, sum;
        String[] abo = new String[a[0].length];
        for (i = 0; i < a.length; i++)
            for (j = 0; j < a[0].length; j++ ) {
                sum = 0;
                int x = a[i][j];
                sum = sum + x;
                if (sum > 62)
                   abo[i] = "Senior";
                else 
                   abo[i] = "Open"; //this would be line 12
            }   
        return abo;
}

    public static void main(String[] args) {
        int [][] a = {{42, 12},{55, 21},{19,-2}};
        String[] x = openOrSenior(a);    //this would be line 20
        Out.print(x);    //here was to see what i'd get if i let it run

    }
}

This is the error i get:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at montecarlo.openOrSenior(montecarlo.java:12) at montecarlo.main(montecarlo.java:20)

I would really appreciate some help.

6
  • It is very simple actually. for (j = 0; j < a[i].length; j++ ) Commented Nov 13, 2017 at 13:42
  • Oh, one more mistake. String[] abo = new String[a.length]; Commented Nov 13, 2017 at 13:43
  • So if I'm 36 years old, and have a handicap of 28, I get a senior membership? Commented Nov 13, 2017 at 13:45
  • 1. Classes should have a capital first letter. 2. Always use brackets with if and for, it improves readability. Commented Nov 13, 2017 at 13:46
  • 1
    Does the data have to be represented as an int[][]? Can you create a class, such as ClubCandidate to hold the age and handicap? Then there would be a List<ClubCandiates> candidates. And a method such as getMembershipType(). This would keep all the data together instead of having to manage separate arrays. Commented Nov 13, 2017 at 13:57

4 Answers 4

2

Here's a more straightforward version with syntax errors fixed.

public class montecarlo {
    static String[] openOrSenior(int[][] a) {
        String[] abo = new String[a.length]; // you had a[0] here instead of a
        for (int i = 0; i < a.length; i++) {
            if (a[i][0] >= 55 && a[i][1] > 7) {
                abo[i] = "Senior";
            } else {
                abo[i] = "Open";
            }
        }
        return abo;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This won't work, as it doesn't decide by the sum of the two.
If you don't have a handicap of 8+ AND age of 55+, you'll never be a senior, based on your code. So it behaves differently than what OP wrote.
@Vlasec "If you don't have a handicap of 8+ AND age of 55+, you'll never be a senior" is the requirement the OP stated. It's right there in the question: If the age is at least 55 and the handicap level is higher than 7 then the person gets a Senior membership if not he gets an Open one. Using the sum is just the method he tried to use, and it doesn't work if, for example, a 53-year old has a handicap of 12.
Ah, I missed that part. Yep, looks like this is an actual solution and OP's code was even more messed up than I thought. Well, using int[] instead of an object with two int fields should have been an indication of that to me :)
Thanks alot, it works now! It was not only this answer alone that got my code working but 2 others in the comments helped as well. This one corrected the way of judging if a person gets senior or open (as you said, the method with the addition wasnt good enough, didnt notice) but without the other answer with the "arrays.aslist" the code wouldnt have worked (Output would be: [Ljava.lang.String;@4554617c). Thats the reason I chose the other one to be the best answer, sry!
0

This fixes all the array bounds problems, but as others mentioned, not the flaws in the semantics. It makes no sense to completely rewrite this answer, so I keep it as it is.

I noticed two major problems in your code that could cause said exception.

On line 12, your problem is, you define the array size wrong.

String[] abo = new String[a.length];

But there is one more problem like that, as the second cycle takes incorrect length, you need:

for (j = 0; j < a[i].length; j++ )

OK, this problem should never happen as all your arrays have same length. But should there be someone with no handicap as just {31} alongside someone with handicap, it crashes in the cycle.

For next practice - create a new class that has two int variables, age and handicap. That is how you do it in Java. An array of integers makes no difference what is age and what is handicap.

Comments

0
import java.util. * ;
import java.lang. * ;
import java.io. * ;

class montecarlo {
    static String[] openOrSenior(int[][] a) {
        int i,
        j,
        sum,
        x;
        /*Declare x here, otherwise in your code it will create a new variable everytime in your for loop*/
        String[] abo = new String[a.length];
        /* a.length not a[0].length */

        for (i = 0; i < a.length; i++) {
            sum = 0;
            /* sum should initialised to zero here every time outside the inner for loop*/

            for (j = 0; j < a[0].length; j++) {

                x = a[i][j];
                sum = sum + x;

            }
            /* check outside for loop whether the total sum is greater than 62 */

            if (sum > 62) {
                abo[i] = "Senior";

            }
            else {
                abo[i] = "Open";

            }

        }
        return abo;

    }

    public static void main(String[] args) {
        int[][] a = {
            {
                42,
                12
            },
            {
                55,
                21
            },
            {
                19,
                -2
            },
            {
                72,
                74
            }
        };
        String[] x = openOrSenior(a);
        System.out.print(x);

    }

}

Here is the working demo of what you wanted. I have pointed out your mistakes in comments. You have done several mistakes, Please watch the code carefully and understand what you did wrong.

Also there is a flaw in your logic which you should fix yourself. for example age=60, handicap level=3 total=60+3=63, which is greater than 62 and hence should be "Senior" according to your logic but it is wrong since the handicap level is below 7.

2 Comments

Dammit, man, pass this through some formatter, it looks uglier than OP's code.
Almost looks like you're paid per line, with the abundance of new lines :) Well, at least it looks way better than the random whitespaces from before.
-1

Try something like

public class montecarlo {
public static void main(String[] args) {
        int [][] a = {{42, 12},{55, 21},{19,-2}};
        String[] x = openOrSenior(a);    //this would be line 20
        System.out.println(Arrays.asList(x));    //here was to see what i'd get if i let it run

    }

    static String[] openOrSenior(int[][] a) {
        int i, j, sum;
        String[] abo = new String[a.length];
        System.out.println(abo.length);
        for (i = 0; i < a.length; i++)
            for (j = 0; j < a[0].length; j++) {
                sum = 0;
                int x = a[i][j];
                sum = sum + x;
                if (sum > 62)
                    abo[i] = "Senior";
                else abo[i] = "Open";
            }   //this would be line 12
        return abo;
    }
}

4 Comments

Whole class posted for the one line fixed. Nothing else fixed, neither the a[0].length part, nor the semantics problem. This answer is just bad and it's just undeserved 25 points. Make it 23 after my downvote. The Arrays.asList is a nice catch, but it still doesn't salvage it.
Dear @Vlasec, with due respect, didn't you see the heading "Try something like". People like you has taken SO as granted and and always showing powers. Just read the questions statement, where he has clearly mentioned "So, I'm new to programming". Every one is not a geek like you, there are many who are novice. I don't care abut your down vote, I have just helped him with whole code since he is a novice. Don't show me your down voting power.
Sorry, maybe I was a bit too harsh. Still, I just dislike how often the OP, being new to programming, chooses the first answer that doesn't have syntax error, doesn't cause runtime errors and that seemingly works, even though in fact, it doesn't meet the requirements.
No need to say sorry. I believe "every heart is different". So No problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.