Results 1 to 16 of 16
  1. #1
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default Urgent help needed for java programming

    Hi Everyone

    I need help in writing a program. Basically this program is supposed to open files dynamically from a folder and extract the parts that i need only.

    Currently what i have is only opening ONE file and only using one delimiter to start the extraction process which is a little off what I need and have to do.

    Would be great if I could get some help here.

    Below is the code that I already have:

    import java.util.Scanner;
    import java.io.*;


    public class Extraction
    {

    private static final String STREAM_NAME = "C:\Users\Royston\Desktop\Proteins\2RQW.pdb";

    public static void main(String[] args)
    {
    Scanner fileScan = null;
    String extraction = null;
    try
    {
    fileScan = new Scanner(Extraction.class.getResourceAsStream(STREA M_NAME));

    // apply the delimiter to the scanner reading the whole file
    fileScan.useDelimiter("MODEL 1");
    while (fileScan.hasNext())
    {
    extraction = fileScan.next();

    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("Extraction Starts Here:");
    System.out.print(extraction);
    System.out.println("Extraction ENDS Here.");

    try{
    // Create file
    FileWriter fstream = new FileWriter("2RQWEXTRACTED.txt");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write(extraction);
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }

    }
    }
    The part of the file I would need is all the lines starting after Model 1 provided if the last column on the right is NOT 'H' and the program should end once it meets ENDMDL. So, How would i need to modify my current program to suit what I need? Help pleaseee.
    MODEL 1
    ATOM 1 N SER A 253 -19.559 -25.512 -41.130 1.00 0.00 N
    ATOM 2 CA SER A 253 -18.749 -26.500 -41.895 1.00 0.00 C
    ATOM 3 C SER A 253 -17.587 -26.979 -41.032 1.00 0.00 C
    ATOM 4 O SER A 253 -16.504 -27.273 -41.538 1.00 0.00 O
    ATOM 5 CB SER A 253 -18.218 -25.843 -43.167 1.00 0.00 C
    ATOM 6 OG SER A 253 -17.271 -24.841 -42.819 1.00 0.00 O
    ATOM 7 H1 SER A 253 -18.928 -24.906 -40.567 1.00 0.00 H
    ATOM 8 H2 SER A 253 -20.212 -26.016 -40.494 1.00 0.00 H
    ATOM 9 H3 SER A 253 -20.104 -24.923 -41.790 1.00 0.00 H
    ATOM 10 HA SER A 253 -19.371 -27.344 -42.159 1.00 0.00 H
    ATOM 11 HB2 SER A 253 -17.735 -26.585 -43.786 1.00 0.00 H
    ATOM 12 HB3 SER A 253 -19.042 -25.399 -43.715 1.00 0.00 H
    ATOM 13 HG SER A 253 -16.398 -25.161 -43.058 1.00 0.00 H

    ATOM 14 N THR A 254 -17.819 -27.053 -39.724 1.00 0.00 N
    ATOM 15 CA THR A 254 -16.783 -27.498 -38.796 1.00 0.00 C
    ATOM 16 C THR A 254 -17.406 -28.192 -37.591 1.00 0.00 C
    ATOM 17 O THR A 254 -17.294 -29.409 -37.438 1.00 0.00 O
    ATOM 18 CB THR A 254 -15.954 -26.301 -38.324 1.00 0.00 C
    ATOM 19 OG1 THR A 254 -16.815 -25.333 -37.740 1.00 0.00 O
    ATOM 20 CG2 THR A 254 -15.220 -25.683 -39.514 1.00 0.00 C
    ATOM 21 H THR A 254 -18.700 -26.805 -39.377 1.00 0.00 H
    ATOM 22 HA THR A 254 -16.130 -28.193 -39.302 1.00 0.00 H
    ATOM 23 HB THR A 254 -15.233 -26.628 -37.592 1.00 0.00 H
    ATOM 24 HG1 THR A 254 -16.416 -25.036 -36.920 1.00 0.00 H
    ATOM 25 HG21 THR A 254 -14.730 -26.462 -40.078 1.00 0.00 H
    ATOM 26 HG22 THR A 254 -14.484 -24.980 -39.156 1.00 0.00 H
    ATOM 27 HG23 THR A 254 -15.928 -25.170 -40.148 1.00 0.00 H

    ATOM 28 N ALA A 255 -18.063 -27.413 -36.738 1.00 0.00 N
    TER 2254 ASP A 407
    ENDMDL
    MASTER 141 0 0 9 0 0 0 6 1137 1 0 12
    END

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    So use a couple of loops. At least try it.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by masijade View Post
    So use a couple of loops. At least try it.

    and inside the loops use string methods like startsWith() or endsWith() for checking your readed lines.

  4. #4
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    Hi I need some help in the reading part..are there any sample codes that I can use??

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Huh? I thought you were already able to read one file? If so, you simply need to do that multiple times. Where's the problem?

  6. #6
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    I need to make it read all the files i have automatically also, my output is not exactly what i needed. as i need it to stop at a point but currently my output only starts from the delimiter until the end of the file, which is not the one i wanted.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Okay? I still see no attempt at it. As hint, create a list of the files, then loop over that list.

    I would, however, seriously suggest moving away from scanner and simply read the file, using a BufferedReader (if it is a "normal" text file), line by line, and checking for the start and end "tags" yourself with the String method matches or contains.

    Now. Using that info, give it a try. You can find plenty of examples of the use of BufferedReader and the mentioned String methods, as well as for loops on the net and/or in Sun's tutorials.

  8. #8
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    I'm trying it out currently however, im thinking of using delimiters instead of loops isit possble to have 2 delimiters (start & stop)? so basically I just have start at the word ATOM and end at ENDMDL

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then keep on trying.

    You seem to not want to even try doing it yourself. It is not hard (even if it is more code, and that should never be your measurement), but you won't find that out (or believe it) until you do it. It is also likely to be much more effecient.

  10. #10
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    There're some changes to what i need to do. Firstly I was told to just write the program to just read the columns right away then extract the lines.

    The program is supposed to extract the whole line only when the last column is not H.

    here is the sample of the file that is supposed to be extracted:
    MODEL 1
    ATOM 1 N SER A 253 -19.559 -25.512 -41.130 1.00 0.00 N
    ATOM 2 CA SER A 253 -18.749 -26.500 -41.895 1.00 0.00 C
    ATOM 3 C SER A 253 -17.587 -26.979 -41.032 1.00 0.00 C
    ATOM 4 O SER A 253 -16.504 -27.273 -41.538 1.00 0.00 O
    ATOM 5 CB SER A 253 -18.218 -25.843 -43.167 1.00 0.00 C
    ATOM 6 OG SER A 253 -17.271 -24.841 -42.819 1.00 0.00 O
    ATOM 7 H1 SER A 253 -18.928 -24.906 -40.567 1.00 0.00 H
    ATOM 8 H2 SER A 253 -20.212 -26.016 -40.494 1.00 0.00 H
    ENDMDL
    MASTER 141 0 0 9 0 0 0 6 1137 1 0 12
    END

    to do that I've tried to used the following code

    Java Code:
    public class MyClass {
    public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("D:/test.txt"));
    ArrayList lines = new ArrayList();
    for(String line = br.readLine();line != null;line = br.readLine()) {
    String[] fields = line.split(" ");
    System.out.println("fields size is "+fields.length);
    System.out.println("fields value is "+fields[0]);
    lines.add(fields);
    }
    String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
    System.out.println("strings are "+strings[0][0]);
    System.out.println("Lines="+strings.length);
    }
    
    }

  11. #11
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by yap_1991 View Post
    There're some changes to what i need to do. ...

    //....
    to do that I've tried to used the following code

    Java Code:
    //.....
    Congrats on the changes in your code! I think that I speak for all in wishing you much success on this project!

  12. #12
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    The current code that i used can only output the word in the first field..how do I make it show the last alphabet on the last column??? I need some help in here.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    To see the last character in a String you could use the String.charAt(len-1) method.

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by yap_1991 View Post
    The current code that i used can only output the word in the first field..how do I make it show the last alphabet on the last column??? I need some help in here.
    Well, hell, why didn't you say so before? It always helps to ask a specific question, ya think?

    You know the length of the array, so you know how to get the last element (hint, the index will be length - 1), right? So get it and test it.

  15. #15
    yap_1991 is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    So do I put that line of code in or it is a new method?

    Java Code:
    public class MyClass {
    public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("D:/test.txt"));
    ArrayList lines = new ArrayList();
    for(String line = br.readLine();line != null;line = br.readLine()) {
    String[] fields = line.split(" ");
    System.out.println("fields size is "+fields.length);
    System.out.println("fields value is "+fields[0]);
    lines.add(fields);
    }
    String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
    System.out.println("strings are "+strings[0][0]);
    System.out.println("Lines="+strings.length);
    }
    
    }

  16. #16
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by yap_1991 View Post
    So do I put that line of code in or it is a new method?
    It doesn't really matter. Just try.

Similar Threads

  1. Urgent Java Help Needed.
    By MaN-GreaSe in forum New To Java
    Replies: 8
    Last Post: 04-22-2010, 11:39 PM
  2. Urgent Help Needed
    By Cid17 in forum New To Java
    Replies: 10
    Last Post: 07-03-2009, 02:42 PM
  3. Urgent Help needed!
    By mlwong in forum New To Java
    Replies: 0
    Last Post: 03-19-2009, 08:51 AM
  4. Urgent help needed here pls!!
    By Manfizy in forum NetBeans
    Replies: 5
    Last Post: 01-28-2009, 07:38 AM
  5. Urgent Java programmer needed.
    By KevinG in forum Jobs Offered
    Replies: 5
    Last Post: 04-23-2008, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •