• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic