very basic

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

very basic

 
0
  #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]
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 62
Reputation: Black Box is on a distinguished road 
Solved Threads: 7
Black Box Black Box is offline Offline
Junior Poster in Training

Re: very basic

 
0
  #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 Quick reply to this message  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Re: very basic

 
0
  #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
  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class GenetiCode
  4. {
  5. //converts the string of characters into array
  6. private static String[] toArray(String dna)
  7. {
  8. String seq[]=new String[dna.length()];
  9. for(int i=0; i<dna.length(); i++)
  10. {char z=dna.charAt(i);
  11. if(true)
  12. {int x=0;
  13. seq[x++]=String.valueOf(z);}}
  14. return seq;}
  15.  
  16. //importing the file
  17. public static void main(String[] Adam) throws IOException
  18. {BufferedReader fileReader=null;
  19. try{fileReader=new BufferedReader(new FileReader("D://dna.txt"));}
  20. catch (IOException e)
  21. {System.out.println("Error: File could not be opened.");}
  22. Scanner scan = new Scanner(System.in);
  23. //asks for the input
  24. System.out.println("What combination would you like to look for? ");
  25. String combi = scan.nextLine();
  26.  
  27. String array[]=new String[combi.length()];
  28. scan.close();
  29.  
  30. String dnas[]=new String[combi.length()];
  31.  
  32. //loop starts here
  33. for(int i=0; i<dnas.length; i++)
  34. {if (dnas[i]==array[0])
  35. {for (int j=0; j<array.length &&(i+j)< dnas.length && dnas[i+j]==array[j]; j++)
  36. {if(j==array.length-1)
  37. {int occur=0;
  38. System.out.println(occur + " position is at " + i);
  39. occur++;
  40. System.out.println("The number of occurence is: " + occur);}}}
  41. else
  42. System.out.println("no match");}}}
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 62
Reputation: Black Box is on a distinguished road 
Solved Threads: 7
Black Box Black Box is offline Offline
Junior Poster in Training

Re: very basic

 
0
  #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.
  1. try {
  2. fileReader=new BufferedReader(new FileReader("D://dna.txt"));
  3. }
  4. catch (IOException e) {
  5. System.out.println("Error: File could not be opened.");
  6. }

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 Quick reply to this message  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Re: very basic

 
0
  #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 Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: very basic

 
0
  #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 Quick reply to this message  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Re: very basic

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

Re: very basic

 
0
  #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 Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: very basic

 
0
  #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 Quick reply to this message  
Join Date: Apr 2007
Posts: 7
Reputation: xanine is an unknown quantity at this point 
Solved Threads: 0
xanine's Avatar
xanine xanine is offline Offline
Newbie Poster

Re: very basic

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC