0

I am doing a computer version of the game called Craps. I ran into some issues displaying some numbers from my array ,can anyone help me please how to display all values in the next array :

int SideBetNumber = Console.readInt("How many side bets would you like to make ? (Introduce a number, minimum 1, maximum 6.)");

int[] SBNArray = new int[SideBetNumber];
int[] sbArray = new int[SideBetNumber];

int s = 0;

for (s = 0; s <= (SideBetNumber -1) ; s++) {
    SBNArray[s] = Console.readInt("On which number would you like to put a side bet ?");
    sbArray[s] = Console.readInt("Currently you have " + money + " chips, how much would you like to bet ?");
    money = money - sbArray[s];
    System.out.println("Thank you for your " +sbArray[s]+ " chip side bet on number " +SBNArray[s]+".");
    System.out.println();
}

PointEstablisher(numberDice ,bet ,money, SBNArray, sbArray, s);

My difficulty is that this is a method I am I passing it to another public static void ...here is my passing :

PointEstablisher(numberDice ,bet ,money, SBNArray, sbArray, s);

and receiving :

public static void PointEstablisher(int numberDice,int bet,
    int money,int[] SBNArray,int[] sbArray, int s)

I know, this is so long, but : How can I display all of the SBNArray and sbArray values, because the index number depends on SideBetNumber, which is introduced by the user ...I can not just type in SBNArray(s) because it gives me error ...or I can not type in SBNArray(3) because it might exist, it might not, depends on the users choice...

I tried this thou, but it did not work :

    if (SBNArray[0] != 0) {
     System.out.println("SBN 1 is " +SBNArray[0]);
    }else {
       System.out.println("NO SBN 1 !!!");
    }
    if (sbArray[0] != 0) {
     System.out.println("sb 1 is " + sbArray[0]);
    }else {
       System.out.println("NO sb 1 !!!");
    }

    if (SBNArray[1] != 0) {
     System.out.println("SBN 2 is " +SBNArray[0]);
    }else {
       System.out.println("NO SBN 2 !!!");
    }
    if (sbArray[1] != 0) {
     System.out.println("sb 2 is " + sbArray[0]);
    }else {
       System.out.println("NO sb 2 !!!");
    }

    if (SBNArray[2] != 0) {
     System.out.println("SBN 3 is " +SBNArray[0]);
    } else {
       System.out.println("NO SBN 3 !!!");
    }

    if (sbArray[2] != 0) {
     System.out.println("sb 3 is " + sbArray[0]);
    }else {
       System.out.println("NO sb 3 !!!");
    }
enter code here

But it did not work out : java.lang.ArrayIndexOutOfBoundException: ...

Can anyone help me display just the existing values of the array ?

0

2 Answers 2

1

You should loop through the array's instead of comparing them one by one.

for (int i = 0; i < SBNArray.length; i++){
    if (sbArray[i] != 0) {
        System.out.println("sb " + i + " is " + sbArray[i]);
    } else {
        System.out.println("NO sb " + i + " !!!");
    }
}

And do the same for the other array. You could even make a private method for this to prevent from repeating yourself.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I just tried it out and it works perfectly, it gives me the values in the arrays and it stops at the end of the array, so no more ArrayIndexOutOfBoundExeption's
0

If the values of SBNArray[] and sbArray[] are correct, and their size is 'SideBetNumber', maybe this will work:(I'm a fresher here,and this is my first answer.If can't help you,I can only say 'Sorry!')

for (int i=0;i<SideBetNumber;i++) {
if (SBNArray[i] != 0) {
    System.out.println("SBN "+(i+1)+" is " +SBNArray[i]);
} else {
    System.out.println("NO SBN "+(i+1)+" !!!");
}
if (sbArray[i] != 0) {
    System.out.println("sb "+(i+1)+" is " + sbArray[i]);
} else {
    System.out.println("NO sb "+(i+1)+" !!!");
}
}

1 Comment

thank you, it is almost the same this that the previous answer, I believe this one too works

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.