aspose file tools*
The moose likes Java in General and the fly likes whats wrong with this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "whats wrong with this code" Watch "whats wrong with this code" New topic
Author

whats wrong with this code

sinchen kutty
Greenhorn

Joined: Sep 13, 2004
Posts: 3
the code bel�ow is compiling and executing without any errors but i couldnot get the expected output.MY input files gene1 and gene2 are as below:

gene1:
EG20012345
EG10023456
EG40025689
EG30036589
EG50012457

gene2:
arg EG100
pdr EG200
frd EG300
fed EG400
fre EG500


import java.io.*;
import java.util.*;
import java.lang.String.*;
public class findreplace
{


public findreplace() throws Exception
{
int ln=0;int ln1=0;
String line=" ";String line1=" ";
String value[] =new String[200];
String values[] =new String[200];
BufferedReader b = new BufferedReader(new FileReader("E:/gene2.txt"));
LineNumberReader l = new LineNumberReader(b);
PrintWriter p=new PrintWriter(new BufferedWriter(new FileWriter("E:/gene3.txt")));
//System.out.println("RMSICA" + "\t"+"Rank"+"\t"+"Consensus Score");
//p.println("BR"+"\t"+"REACTANTS" );
while(true)
{
line = l.readLine();if (line==null) break;
StringTokenizer st = new StringTokenizer(line);
ln = l.getLineNumber();
if (ln>=1)
{
int i = 0;
while(st.hasMoreTokens())
{
value[i] = st.nextToken();//p.println(values[i]);

//if(values[0].equals("UNIQUE-ID"))
if(i==1)
{
BufferedReader b1 = new BufferedReader(new FileReader("E:/gene1.txt"));
LineNumberReader l1 = new LineNumberReader(b1);
while(true)
{
line1 = l1.readLine();if (line1==null) break;
StringTokenizer st1 = new StringTokenizer(line1);
ln1 = l1.getLineNumber();
if (ln1>=1)
{
int i1 = 0;
while(st1.hasMoreTokens())
{
values[i1] = st1.nextToken();
if (value[1].equals(values[0]))
if(i1==1)
p.println();
p.print("replaced file");
p.print(value[0]+"\t"+value[1]+"\t"+values[1]);
}

i1++;
}


}

}
}
i++;
}
}

p.close();
}
public static void main(String s[]) throws Exception
{
new findreplace();
}
}

My expected output:

Gene3(file):
arg EG100 23456
pdr EG200 12345
frd EG300 36589
fed EG400 25689
fre EG500 12457.

thanx in advace,
regards,
Bobby
Kip Dekan
Greenhorn

Joined: Apr 22, 2003
Posts: 9
Hi,

try this one. In general it's your Code I have taken only small changes.
Perhaps it's not the best way to code that but it will work.

Have fun with.


import java.io.*;
import java.util.*;
import java.lang.String.*;
public class findreplace
{


public findreplace() throws Exception
{
int ln=0;int ln1=0;
String line=" ";String line1=" ";
String value[] =new String[200];
String values[] =new String[200];

BufferedReader b = new BufferedReader(new FileReader("E:/gene2.txt"));
LineNumberReader l = new LineNumberReader(b);

PrintWriter p=new PrintWriter(new BufferedWriter(new FileWriter("E:/gene3.txt")));

while(true)
{
line = l.readLine();if (line==null) break;
StringTokenizer st = new StringTokenizer(line);
ln = l.getLineNumber();
if (ln>=1)
{
int i = 0;
while(st.hasMoreTokens())
{
value[i] = st.nextToken();

if(i==1)
{
BufferedReader b1 = new BufferedReader(new FileReader("E:/gene1.txt"));
LineNumberReader l1 = new LineNumberReader(b1);
while(true)
{
line1 = l1.readLine();if (line1==null) break;

StringTokenizer st1 = new StringTokenizer(line1);
ln1 = l1.getLineNumber();

if (ln1>=1)
{
int i1 = 0;
while(st1.hasMoreTokens())
{
values[i1] = st1.nextToken();
i1++;
}

if (value[1].equals(values[0]))
{
p.println();
p.print(value[0]+"\t"+value[1]+"\t"+values[1]);
}

}

}
}
i++;
}
}
}

p.close();
}

public static void main(String s[]) throws Exception
{
new findreplace();
}
}
sinchen kutty
Greenhorn

Joined: Sep 13, 2004
Posts: 3
thanx alot,i have fixed the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats wrong with this code
 
Similar Threads
java.io.IOException Too many open files error
where this program goes wrong(using 2D array)?
Finding a particular pattern in a file
Pls fix this program
find and replace