V
Vasu
Hi Friends!
Can anybody tell me why the following code generating
ArrayIndexOutofBoundsException = 5
import java.lang.System;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
public class Bingo1 {
public static void main(String[] args) throws IOException {
System.out.println("Generating 5 Random Nos. for evaluation");
int[] In = new int[5];
int[] In2= new int[2];
int a=0, b=0;
boolean m=false;
boolean n=false;
//Generates random no in the range of 0 - 40 and put in an array.
for(int i=0; i<5; i++){
Random randomNo = new Random();
In=randomNo.nextInt(40);
System.out.println(In); }
//Asks user to enter two digits to search in the Array.
for(int i=0; i<2; i++){
if(i==0) {System.out.print("Please Enter first no.: ");}
else {System.out.print("Please Enter second no.: ");}
System.out.flush();
String y;
DataInputStream keyboardInput = new DataInputStream(System.in);
y=keyboardInput.readLine();
if(i==0){a = Integer.valueOf(y).intValue();
System.out.println("This is first digit : "+a); }
else {b = Integer.valueOf(y).intValue();
System.out.println("This is second digit : "+b);}
}
/*Now code compares the two digits against the array and sets boolean
value accordingly */
System.out.println("Let's compare the two digits you mentioned");
for(int i=0; i<5; i++){
if(In==a){ m=true;} else {m=false;} }
for(int j=0; j<=5; j++){
if(In[j]==b){n=true;} else {n=false;} }
System.out.println("Value of m = "+m);
System.out.println("Value of n = "+n);
if(m==true && n==true){System.out.println("Bingo! You have made
it.");}
else {System.out.println("Sorry! try again.");}
}
}
Now this code, when I'm putting the two digits which are there in the
array, clearly tells "Bingo!...", but when I put one digit different
than in array (particularly the second one) it shows the following
message :
C:\jdk1.6\bin>java Bingo1
Generating 5 Random Nos. for evaluation
13
15
13
12
13
Please Enter first no.: 14
This is first digit : 14
Please Enter second no.: 13
This is second digit : 13
Let's compare the two digits you mentioned
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
Why?? Please help make me understand the root cause.
Thanks and regards
Vasu
at Bingo1.main(Bingo1.java:52)
Can anybody tell me why the following code generating
ArrayIndexOutofBoundsException = 5
import java.lang.System;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
public class Bingo1 {
public static void main(String[] args) throws IOException {
System.out.println("Generating 5 Random Nos. for evaluation");
int[] In = new int[5];
int[] In2= new int[2];
int a=0, b=0;
boolean m=false;
boolean n=false;
//Generates random no in the range of 0 - 40 and put in an array.
for(int i=0; i<5; i++){
Random randomNo = new Random();
In=randomNo.nextInt(40);
System.out.println(In); }
//Asks user to enter two digits to search in the Array.
for(int i=0; i<2; i++){
if(i==0) {System.out.print("Please Enter first no.: ");}
else {System.out.print("Please Enter second no.: ");}
System.out.flush();
String y;
DataInputStream keyboardInput = new DataInputStream(System.in);
y=keyboardInput.readLine();
if(i==0){a = Integer.valueOf(y).intValue();
System.out.println("This is first digit : "+a); }
else {b = Integer.valueOf(y).intValue();
System.out.println("This is second digit : "+b);}
}
/*Now code compares the two digits against the array and sets boolean
value accordingly */
System.out.println("Let's compare the two digits you mentioned");
for(int i=0; i<5; i++){
if(In==a){ m=true;} else {m=false;} }
for(int j=0; j<=5; j++){
if(In[j]==b){n=true;} else {n=false;} }
System.out.println("Value of m = "+m);
System.out.println("Value of n = "+n);
if(m==true && n==true){System.out.println("Bingo! You have made
it.");}
else {System.out.println("Sorry! try again.");}
}
}
Now this code, when I'm putting the two digits which are there in the
array, clearly tells "Bingo!...", but when I put one digit different
than in array (particularly the second one) it shows the following
message :
C:\jdk1.6\bin>java Bingo1
Generating 5 Random Nos. for evaluation
13
15
13
12
13
Please Enter first no.: 14
This is first digit : 14
Please Enter second no.: 13
This is second digit : 13
Let's compare the two digits you mentioned
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
Why?? Please help make me understand the root cause.
Thanks and regards
Vasu
at Bingo1.main(Bingo1.java:52)