944,199 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1858
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 3rd, 2007
0

very basic

Expand Post »
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.
java Syntax (Toggle Plain Text)
  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]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xanine is offline Offline
7 posts
since Apr 2007
Dec 4th, 2007
0

Re: very basic

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
Reputation Points: 60
Solved Threads: 7
Junior Poster in Training
Black Box is offline Offline
62 posts
since Nov 2007
Dec 6th, 2007
0

Re: very basic

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
Java Syntax (Toggle Plain Text)
  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");}}}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xanine is offline Offline
7 posts
since Apr 2007
Dec 6th, 2007
0

Re: very basic

Click to Expand / Collapse  Quote originally posted by xanine ...
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.
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 60
Solved Threads: 7
Junior Poster in Training
Black Box is offline Offline
62 posts
since Nov 2007
Dec 9th, 2007
0

Re: very basic

..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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xanine is offline Offline
7 posts
since Apr 2007
Dec 10th, 2007
0

Re: very basic

what you wanting to do here? You wanting to store a string or a single char at a time into an array index?
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 10th, 2007
0

Re: very basic

i want to store a string in an array.. the DNA sequence
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xanine is offline Offline
7 posts
since Apr 2007
Dec 11th, 2007
0

Re: very basic

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



String array[100];
String theStringYouWantToStore;

array[index] = theStringYouWantToStore
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 12th, 2007
0

Re: very basic

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.
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007
Jan 10th, 2008
0

Re: very basic

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()))
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xanine is offline Offline
7 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: general query procedure
Next Thread in Java Forum Timeline: NullPointerexception ??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC