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.

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]);

            }

        }

}

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

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");}}}

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

..i've already read the file(thanks a lot) but then my problem now is how to store it in the array.. i had difficulty in implemeting it.

what you wanting to do here? You wanting to store a string or a single char at a time into an array index?

i want to store a string in an array.. the DNA sequence

Easy create an Array of strings and then add the string to the array index...

String array[100];
String theStringYouWantToStore;

array[index] = theStringYouWantToStore

keep in mind, that you're using a command line parameter to get the volume of the array... that may not be the easiest way to work with, since you have to know in front how many rows there are in your input file.

after one year.. hmm.
i got the code already but another problem is..
it cant' read the file when it's in caps lock form? how will i implement it? i tried this but it didn't go thru the for loop..

if (combi.equals(combi.toLowerCase()) || combi.equals(combi.toUpperCase()))

if (combi.equals(combi.toLowerCase()) || combi.equals(combi.toUpperCase()))

This statement in plain english reads:

if thisString equals thisStringConvertedToLowerCase or thisStringConvertedToUpperCase then...

This will ONLY be true if the entire string is written in lower or upper case. If there is any other combination of upper and lower case letters in your string it will return false.

I'm not 100% sure of what you are trying to do here...

because the texts or the letters in the textfile are in small caps.. it won't read the textfile when i will enter a string of uppercase letters.. can i use the equalsIgnoreCase?

hey! i got the code already! it's now solved. But another problem here is how to implement it in linked list.. i really don't know how to start it.. but i do have some sample source codes about it..geesh.. does it have to implement some constructors first? how then it could be called in the main method?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.