-3
class A {
public static void main (String[] args) {
   // code ...
   System.out.println(B.fun1());
   // code ...
}

class B {
   // code ...
   public int fun1(){
   return C.fun2();
 }

class C {
    // code ...
    public int fun2() {
        int u = 0;
        for(int k= 0; arr[k] != null ; k++,System.out.println("k="+k)) {
            int a = arr[k].getVal();
            String s = Integer.toString(a);
            if (s.equals(("1"))) {
                u = u + 10;
            } else {
                u = u + a;
            }
        }
        return u;
    }
 }

I have a code of same structure as shown above and when main executes the output is:

k=1
k=2
k=3

the return value u from fun1 as printed in main is:

30  //depends on getVal()

but after this output it also shows this

k=1
k=2
k=1
k=2
k=1
k=2

and I receive an ArrayIndexOutOfBoundsException. How is this possible? My question is why I am getting this k=1 k=2 k=1 ... thing CODE:https://drive.google.com/open?id=1aZYv7qqYd1___fXA92lbtLO5WccI9wz6 How to reproduce: Things are random so try to play couple of times.

FOX
  • 1
  • 3
  • Please create a [mcve] which we can run to replicate the issue. – 4castle Dec 24 '17 at 18:59
  • 7
    Its probably because `arr[k] != null` – Aniket Sahrawat Dec 24 '17 at 18:59
  • `arr[k] != null` with an out of bound index would cause an error in most languages because you have to first fetch `arr[k]` before you can compare it. if k index is out of bounds then error. – JoshKisb Dec 24 '17 at 19:02
  • Just as @AniketSahrawat mentioned, you are not checking whether or not you exceeded the array limits in your `for(..; arr[k] != null; ...)`. What's stopping `k` from going over the size of the array? Try checking if you hit the array length `for(...; k < arr.length; ...)` – StaticBeagle Dec 24 '17 at 19:04
  • it is like arr[0]=null; arr[1]=null;... and when i call this it update like arr[0]=10;arr[1]=7;arr[2]=null,arr[3]=null... and i also tried placing limit to k to maximum value of array size like ..for(int k= 0; arr[k] != null && k<100 ; k++,System.out.println("k="+k)) {... still same problem – FOX Dec 24 '17 at 19:10
  • And i cant create a Minimal, Complete, and Verifiable example as this is a project having 40s of classes – FOX Dec 24 '17 at 19:17
  • @4castle please read the question heading "for loop behaving differently in java" so why are you all relating this question to arrayindexoutofbound and declare it duplicate.Are you in hurry? I am not a noob coder i have petty 5 years of coding experience. I came to stack to get my answer not to get my questing declared duplicate. – FOX Dec 24 '17 at 19:59
  • You haven't supplied enough code for us to replicate the issue, so it should have been closed for not providing a [mcve], but instead it was closed as a duplicate so that it would be of more help to you until you edit the question to make it answerable. – 4castle Dec 24 '17 at 20:06
  • i said its a project so it take times to bring minimal code. And my question is different than the suggested duplicate question. – FOX Dec 24 '17 at 20:07
  • @4castle i added my questing to gain focus on my question and whole code and please remove wrong duplicate question and i want answer not closed question please remove this duplicate thing – FOX Dec 24 '17 at 20:25

2 Answers2

1
for(int k= 0; arr[k] != null ; k++,System.out.println("k="+k)) {

You have no constraint on how high k can go, rather you are only evaluating until it is found to be null. Out of index values will throw an IndexOutOfBoundsException rather than evaluate to a null value, so if you have no null values in your array, you will always run into an exception barring some other manner of breaking the loop. To protect from this you can check to make sure k is within the array length prior to doing your null check.

for(int k= 0; k < arr.length && arr[k] != null ; k++,System.out.println("k="+k)) {
msg45f
  • 695
  • 11
  • 21
  • i know that and i said in previous comments many times it didnt worked – FOX Dec 24 '17 at 19:43
  • Then you must also be modifying k outside of the loop definition, causing it to behave in an unusual way. This is really not recommendable practice. – msg45f Dec 24 '17 at 19:47
  • it's a local variable – FOX Dec 24 '17 at 19:50
  • This variables can still be modified within the loop body. Regardless, if you have the proper constraint on array length in the loop definition, you should never get an out of bounds exception on that line. Your results {1, 2, 1, 2, ...} are indicative of either modifying the index tracking value or the loop(s) running multiple times in succession - in which case it's likely the obvious matter: one of your several arrays does not contain a null value and therefore will not terminate one of your loops properly. Again, this should be completely avoidable with the proper loop constraint. – msg45f Dec 24 '17 at 20:04
  • code is out why not check it yourself. :-) – FOX Dec 24 '17 at 20:06
0

The original code is here

Looking to your entire code I guessed how to remove the extra numbers in console output, also I made a couple change to make your code a bit more efficient. Let me know if this is the final solution to your problem.

First of all I removed the System.out.Println(k) inside the loop which eliminated the final extra numbers in the console output:

public int evaluateHand() {
    int u = 0;
    try {
        for(int k= 0; hand[k] != null; k++) {
            int a = hand[k].getRank();
            String ok = Integer.toString(a);
            if (ok.equals(("13"))) {
                u = u + 10;
            }else if (ok.equals(("12"))) {
                u = u + 10;
            } else if (ok.equals(("11"))) {
                u = u + 10;
            } else if (ok.equals(("1"))) {
                u = u + 11;
            } else {
                u = u + a;
            }
        }
Alex Cuadrón
  • 638
  • 12
  • 19