Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes where this program goes wrong(using 2D array)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where this program goes wrong(using 2D array)?" Watch "where this program goes wrong(using 2D array)?" New topic
Author

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

richardgonzales muller
Greenhorn

Joined: Sep 12, 2005
Posts: 4
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.io.*;
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;

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

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;}
if(B1!=0&&C1!=0)
}
{p.println(A1+"\t"+B1+"\t"+C1);}
}}}p.close();*/
}
public static void main(String s[]) throws Exception
{
new motif_analysis1();
}
}


Thanks in advance
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
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


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where this program goes wrong(using 2D array)?
 
Similar Threads
Pls fix this program
java.io.IOException Too many open files error
Finding a particular pattern in a file
find and replace
whats wrong with this code