Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

where this program goes wrong(using 2D array)?  RSS feed

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hella everybody,

I have written a program. The motive of this program is to find a pattern. my pattern is 'a' linked with both 'b' and 'c', the it should write to the output file. for example in my input file:

1 2
1 3
1 5
2 4
2 6.
3 5
MY output would be
1 2 3
1 2 5
1 3 5
2 4 6.

But as such my input text file has 2636 rows and 2 columns.

I have used 2D array which stores the tokens of my text file and then started my main loop. Cos this is the suggestion given by an expert in this forum to speed up the program vastly. now i am getting Array Out of bound Exception and also when I am comparing the values in my 2D array with the loop elements, it gives me a error. PLs fix up this code.

import java.util.*;
import java.lang.String.*;
public class motif_analysis1
public motif_analysis1() throws Exception
int ln=0;int ln1=0;int count=0;//int a,b,c=0;
String line,line1,A,B = " ";
int [][]x = new int[2636][2];
String value[] =new String[200];
String values[] =new String[200];
BufferedReader b1 = new BufferedReader(new FileReader("E:/Final_files/motif_analysis/Transreg_Motif.txt"));
PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter("E:/Final_files/motif_analysis/motif1_output.txt")));
LineNumberReader l = new LineNumberReader(b1);
int A1=0;int B1=0;int C1=0;

line = l.readLine();if (line==null) break;
StringTokenizer st = new StringTokenizer(line);
ln = l.getLineNumber();
if (ln>=1)
int i = 0;int j=1;
value[i] = st.nextToken();
x[ln][i+1] = Integer.parseInt(value[i]);p.print(x[ln][i+1]+"\t");
} j++;

for(int a=1; a<900; a++){
for (int b=1; b<1280; b++){
for (int c=b+1; c<1280; c++){
for (int j=1; j<=2636;j++)
if((a==x[j][1])&& (b== x[j][2]))
{A1=a; B1=b;/*p.println(A1+"\t"+B1+"\t");}
if((a==x[j][1])&& (c== x[j][2]))
{A1=a; C1=c;}
public static void main(String s[]) throws Exception
new motif_analysis1();

Thanks in advance
Posts: 3742
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array indexing starts at 0, therefore if you have an array of size 2636, the last element will be at index 2635.
This linewill attempt to access array index 2636, which does not exist. Replace it with
Fire me boy! Cool, soothing, shameless self promotion:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!