•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,801 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1079 | Replies: 12
![]() |
i know this could be very simple for you guys but i do have a problem with this.
The program searches for a particular array from a text file(i don't know how to import a text file too) and counts the number of occurrence in it. The text file is about DNA sequence. The error in the program says that it has illegal start of expression and i don't know what to replace it or merely i don't actually know what the problem is..hope you guys can help me..thanks a lot.. from a newbie.
The program searches for a particular array from a text file(i don't know how to import a text file too) and counts the number of occurrence in it. The text file is about DNA sequence. The error in the program says that it has illegal start of expression and i don't know what to replace it or merely i don't actually know what the problem is..hope you guys can help me..thanks a lot.. from a newbie.
java Syntax (Toggle Plain Text)
[inlinecode] import java.io.*; import java.util.Scanner; public class GenetiCode { //main method public static void main(String[]code) throws IOException { FileReader file = new FileReader("D:\\dna.txt"); BufferedReader fileInput = new BufferedReader(file); try { PrintStream outStream = new PrintStream(new FileOutputStream("D:\\dna.txt")); outStream.close(); } catch (IOException e) { System.out.println("Error: File could not be opened."); } Scanner scan = new Scanner(System.in); String dna=null; String dnas[]=new String[5]; //asks for the input System.out.println("What combination would you like to look for? "); String combi = scan.nextLine(); String[] array=new String[combi.length()]; scan.close(); array=toArray(combi); //loop starts here for(int i=0; i<dnas.length; i++) { if (dnas[i]==array[0]) { for (int j=0; j<array.length &&(i+j)< dnas.length && dnas[i+j]==array[j]; j++) { if(j==array.length-1) { int occur=0; occur+=1; System.out.println(occur + " position is at " + i); } } } System.out.println("The number of occurence is: " + occur); printArray(dnas); printArray(array); } //converts it to array private static String[] toArray(String dna) { String seq[]=new String[dna.length()]; int x=0; for(int i=0; i<dna.length(); i++) { char z=dna.charAt(i); if(true) { System.out.println("Char z=" + z); seq[x++]=String.valueOf(z); } } return seq; } //prints the array public static void printArray(String dnas[]) { for(int i=0; i<dnas.length; i++) { System.out.println("DNA[" + i + "]=" + dnas[i]); } } }[/inlinecode]
•
•
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation:
Rep Power: 1
Solved Threads: 6
Ok, first things first... the way your code looks on this forum made my eyes hurt while trying to figure out what it does. Try to use less newline and shorter indents.
As for the error, normally it comes with a line number so you should know at what line of code it errors.
An other thing, I see that in your for loops you try to match the string given by the user (i.e. combi) with one from the strings in the array dnas. Now, in your code the only thing that has happened to that array of strings is the following:
String dnas[] = new String[5];
That means, there are no strings in that array, you've never put anything in there.
This might not solve all your problems, but try to work with what I said and when you're stuck again, post a prettier version of the code please. It'll be easier to go through it.
Black Box
As for the error, normally it comes with a line number so you should know at what line of code it errors.
An other thing, I see that in your for loops you try to match the string given by the user (i.e. combi) with one from the strings in the array dnas. Now, in your code the only thing that has happened to that array of strings is the following:
String dnas[] = new String[5];
That means, there are no strings in that array, you've never put anything in there.
This might not solve all your problems, but try to work with what I said and when you're stuck again, post a prettier version of the code please. It'll be easier to go through it.

Black Box
geez.. I'm sorry it caused you a lot of trouble..actually it's my first post. Here i posted a "prettier" code.. the program ran though but the output is not correct.. it didn't read the file at all.. i really don't know how to import a file.. is my importing correct?? by the way.. thanks for replying... i really need it badly 

import java.io.*;
import java.util.Scanner;
public class GenetiCode
{
//converts the string of characters into array
private static String[] toArray(String dna)
{
String seq[]=new String[dna.length()];
for(int i=0; i<dna.length(); i++)
{char z=dna.charAt(i);
if(true)
{int x=0;
seq[x++]=String.valueOf(z);}}
return seq;}
//importing the file
public static void main(String[] Adam) throws IOException
{BufferedReader fileReader=null;
try{fileReader=new BufferedReader(new FileReader("D://dna.txt"));}
catch (IOException e)
{System.out.println("Error: File could not be opened.");}
Scanner scan = new Scanner(System.in);
//asks for the input
System.out.println("What combination would you like to look for? ");
String combi = scan.nextLine();
String array[]=new String[combi.length()];
scan.close();
String dnas[]=new String[combi.length()];
//loop starts here
for(int i=0; i<dnas.length; i++)
{if (dnas[i]==array[0])
{for (int j=0; j<array.length &&(i+j)< dnas.length && dnas[i+j]==array[j]; j++)
{if(j==array.length-1)
{int occur=0;
System.out.println(occur + " position is at " + i);
occur++;
System.out.println("The number of occurence is: " + occur);}}}
else
System.out.println("no match");}}} •
•
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation:
Rep Power: 1
Solved Threads: 6
•
•
•
•
It didn't read the file at all.. i really don't know how to import a file.. is my importing correct?
Ok, it's still the same error as before.
try {
fileReader=new BufferedReader(new FileReader("D://dna.txt"));
}
catch (IOException e) {
System.out.println("Error: File could not be opened.");
}In that try block you contact the file you want to read and eventually the strings in there should be stored in the array dnas. But, you never tell Java to read from the file, let alone tell it to store it in dnas. You have only initialized the filereader in the try-block shown above, it is ready to read from the file, but you just don't do it.
Checkout the Java API for class FileReader to see how it works, it's fairly simple. Should you have questions, post them here.
And just a hint, since your reader is initialized in a try-block like it should, you should do the reading from the file (and the storing into dnas) in that same block, because your reader is only locally initialized.
Black Box
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- Basic PHP Includes (PHP)
- was windows made with BASIC? (Legacy and Other Languages)
- The Move.....Visual Basic 6, Visual Basic .NET ? (VB.NET)
- Encrypting a Visual Basic application (Visual Basic 4 / 5 / 6)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
- Visual Basic.net (VB.NET)
Other Threads in the Java Forum
- Previous Thread: general query procedure
- Next Thread: NullPointerexception ??


Linear Mode