User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Help very basic

  #1  
Dec 3rd, 2007
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.
  1. [inlinecode]
  2.  
  3.  
  4.  
  5. import java.io.*;
  6.  
  7. import java.util.Scanner;
  8.  
  9.  
  10.  
  11. public class GenetiCode
  12.  
  13. {
  14.  
  15. //main method
  16.  
  17. public static void main(String[]code) throws IOException
  18.  
  19. {
  20.  
  21.  
  22.  
  23. FileReader file = new FileReader("D:\\dna.txt");
  24.  
  25. BufferedReader fileInput = new BufferedReader(file);
  26.  
  27.  
  28.  
  29. try
  30.  
  31. {
  32.  
  33. PrintStream outStream = new PrintStream(new FileOutputStream("D:\\dna.txt"));
  34.  
  35. outStream.close();
  36.  
  37. }
  38.  
  39. catch (IOException e)
  40.  
  41. {
  42.  
  43. System.out.println("Error: File could not be opened.");
  44.  
  45. }
  46.  
  47.  
  48.  
  49. Scanner scan = new Scanner(System.in);
  50.  
  51.  
  52.  
  53. String dna=null;
  54.  
  55. String dnas[]=new String[5];
  56.  
  57.  
  58.  
  59. //asks for the input
  60.  
  61. System.out.println("What combination would you like to look for? ");
  62.  
  63. String combi = scan.nextLine();
  64.  
  65.  
  66.  
  67. String[] array=new String[combi.length()];
  68.  
  69. scan.close();
  70.  
  71.  
  72.  
  73. array=toArray(combi);
  74.  
  75.  
  76.  
  77. //loop starts here
  78.  
  79. for(int i=0; i<dnas.length; i++)
  80.  
  81. {
  82.  
  83. if (dnas[i]==array[0])
  84.  
  85. {
  86.  
  87. for (int j=0; j<array.length &&(i+j)< dnas.length && dnas[i+j]==array[j]; j++)
  88.  
  89. {
  90.  
  91. if(j==array.length-1)
  92.  
  93. {
  94.  
  95. int occur=0;
  96.  
  97. occur+=1;
  98.  
  99. System.out.println(occur + " position is at " + i);
  100.  
  101. }
  102.  
  103. }
  104.  
  105. }
  106.  
  107. System.out.println("The number of occurence is: " + occur);
  108.  
  109.  
  110.  
  111. printArray(dnas);
  112.  
  113. printArray(array);
  114.  
  115. }
  116.  
  117.  
  118.  
  119. //converts it to array
  120.  
  121. private static String[] toArray(String dna)
  122.  
  123. {
  124.  
  125. String seq[]=new String[dna.length()];
  126.  
  127. int x=0;
  128.  
  129.  
  130.  
  131. for(int i=0; i<dna.length(); i++)
  132.  
  133. {
  134.  
  135. char z=dna.charAt(i);
  136.  
  137. if(true)
  138.  
  139. {
  140.  
  141. System.out.println("Char z=" + z);
  142.  
  143. seq[x++]=String.valueOf(z);
  144.  
  145. }
  146.  
  147. }
  148.  
  149. return seq;
  150.  
  151. }
  152.  
  153.  
  154.  
  155. //prints the array
  156.  
  157. public static void printArray(String dnas[])
  158.  
  159. {
  160.  
  161. for(int i=0; i<dnas.length; i++)
  162.  
  163. {
  164.  
  165. System.out.println("DNA[" + i + "]=" + dnas[i]);
  166.  
  167. }
  168.  
  169. }
  170.  
  171. }[/inlinecode]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: very basic

  #2  
Dec 4th, 2007
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
Reply With Quote  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Help Re: very basic

  #3  
Dec 6th, 2007
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");}}}  
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: very basic

  #4  
Dec 6th, 2007
Originally Posted by xanine View Post
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
Reply With Quote  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Re: very basic

  #5  
Dec 9th, 2007
..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.
Reply With Quote  
Join Date: Dec 2004
Posts: 469
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: very basic

  #6  
Dec 10th, 2007
what you wanting to do here? You wanting to store a string or a single char at a time into an array index?
Reply With Quote  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Re: very basic

  #7  
Dec 10th, 2007
i want to store a string in an array.. the DNA sequence
Reply With Quote  
Join Date: Dec 2004
Posts: 469
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: very basic

  #8  
Dec 11th, 2007
Easy create an Array of strings and then add the string to the array index...



String array[100];
String theStringYouWantToStore;

array[index] = theStringYouWantToStore
Reply With Quote  
Join Date: Jan 2007
Posts: 430
Reputation: stultuske will become famous soon enough stultuske will become famous soon enough 
Rep Power: 3
Solved Threads: 45
stultuske's Avatar
stultuske stultuske is online now Online
Posting Pro in Training

Re: very basic

  #9  
Dec 12th, 2007
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.
Reply With Quote  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Re: very basic

  #10  
Jan 10th, 2008
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()))
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC