0

have a small problem here.. when i tried to add more conditions to my IF it doesnt give the expected output..

if (str.startsWith("HEADER") || str.startsWith("TITLE") || (str.startsWith("ATOM") && !str.endsWith("H")))

it does the correct thing for HEADER and TITLE but as for the last one ATOM part it gives me everything, including those that ends with H..did i do anything wrong?

0

Can you show the str values that are passing the tests you show in the if statement that you think should NOT pass the test?
The tests will pass 3 different types of lines.

0

the output of the program gives only 2 correct types..the startsWith ATOM and !str.endsWith H part is wrong as it still gives me the line that contains H when it is not supposed to. am i wrong in putting the codes this way?? but it works fine without the HEADER and TITLE part.

0

Can you show the ACTUAL data that is passing the test?

Here's a sample program showing how you should test your code:

// Show how to test

public class ShowHowToTest {

  public static void main(String[] args) {
      String[] data = {"ATOM H", "ATOM Q", "HEADER H", "TITLE H"};  // Sample input

      for(int i=0; i < data.length; i++) {
         String str = data[i];
         // Put your if code there to test it:
         if (str.startsWith("HEADER") || str.startsWith("TITLE") 
             || (str.startsWith("ATOM") && !str.endsWith("H")))
            System.out.println("Passed:" + str);
         else
            System.out.println("Failed:" + str);
      } // end for(i)
  }
}/* Output:
Failed:ATOM H
Passed:ATOM Q
Passed:HEADER H
Passed:TITLE H
*/

Edited by NormR1: n/a

0

I've tested according to the codes you've given me and it displays the same thing as what happened to my output, the last that ends with H still appears.. How do i go about rectifying the problem?

here are my codes:

import java.io.*;

public class extracttion
{
      public static void main( String[] args )
      {
     File dir = new File("2KQ6.pdb"); 					//set file
     String path = dir.getAbsolutePath();				//get file path
     int lala = path.lastIndexOf("\\");					//get index of '\'
     String howPath = path.substring(0,lala+1);			//extract directory name
     File usePath = new File(howPath);					//assign directory for reading file
     String[] fileDir = usePath.list();					//store file in dit to array
     if(fileDir == null) 								//check if dir exists
     {
     	System.out.println("Directory Does Not Exist");
     }
     else
     {
     	for(int j=0; j<fileDir.length-2;j++)
     	{
     		String fileName = fileDir[j];
      try 
      	{ 		
       	BufferedReader in = new BufferedReader(new FileReader(fileDir[j]));
FileWriter outStream = new FileWriter ("I:\\MP Project\\Atom" +fileName); 
     	String str;

      		while ((str = in.readLine()) != null) 
      		{
         if (str.startsWith("HEADER") || str.startsWith("TITLE") 
             || (str.startsWith("ATOM") && !str.endsWith("H")))
      			{
      			System.out.print(str);
      		outStream.write(str);	
      	      	
      			
      			}
      		}

     	 outStream.close();
   		in.close(); 	
   		} catch (IOException e) { }    
    
     	}
     }
      }
}
0

Please change my program to show what you are saying. Compile it, execute it and copy and paste the output here.

Did my program function the way you wanted?

0

I've changed the program to what i needed but it still doesnt meet what i need as the line containing the last character H appears in the output.

this is what i input and this is my output as well. the last line is not suppose to be in the output since it ends with H

HEADER SIGNALING PROTEIN 15-JUN-09 2KK2,TITLE NMR SOLUTION STRUCTURE OF THE PHEROMONE EN-A1 FROM EUPLOTES

ATOM 11 CZ TYR A 1 -8.350 7.693 5.350 1.00 0.00 C ATOM 12 OH TYR A 1 -7.008 7.782 5.527 1.00 0.00 O ATOM 13 H1 TYR A 1 -14.458 8.768 2.070 1.00 0.00 H

0

for a better view of my output:

HEADER SIGNALING PROTEIN 15-JUN-09 2KK2
TITLE NMR SOLUTION STRUCTURE OF THE PHEROMONE EN-A1 FROM EUPLOTES

ATOM 11 CZ TYR A 1 -8.350 7.693 5.350 1.00 0.00 C ATOM 12 OH TYR A 1 -7.008 7.782 5.527 1.00 0.00 O ATOM 13 H1 TYR A 1 -14.458 8.768 2.070 1.00 0.00 H

0

I think I know where the problem lies..

basically it doesnt work because of this line, on its own it doesnt work too.
if (str.startsWith("ATOM") && !str.endsWith("H"))

it seems correct to me but i dont know why it just doesnt work. does anyone know why?

0

Can you put those two lines in my small program, compile it, execute it and copy and paste the console output here?

Or change your program as follows:

while ((str = in.readLine()) != null) 
      		{
            System.out.println("str=" + str);  // show what is being tested
            if (str.startsWith("HEADER") || str.startsWith("TITLE") 
                || (str.startsWith("ATOM") && !str.endsWith("H")))
      			{
      			System.out.print("Passed: " + str);
      		outStream.write(str);	
 
 
      			}
      		}

Execute it and copy and paste the console output here.

Edited by NormR1: n/a

0

Is there whitespace at the end of the string?

Try with str.trim().starts... and str.trim().ends...

Or do str = str.trim()

0

Thanks masijade!(: Didnt realise that there's whitespace until you brought it up.After all the rectification, I've managed to complete it.Thank you NORM!

0

actually i have another question to ask. would like to know how do i incorporate my current code and ftp together? in the sense that ftp will retrieve the files from a source and my codes will then do the necessities. do i just put in the ftp codes into my current codes?

0

For HTTP use a browser to request a file.
For FTP find an FTP program to use.
Read the documentation.

0

err when i tried my program on a different computer, the output shown on console has a perfect alignment, but when i open the new file created, the alignment is all jumbled up, is there a away to fix this problem??

0

Well, the console is probably using a courier font, and the "file editor", whichever it is, is probably using another font.

Or, if you mean because there are no "newlines" in the file, then add a call to fileWriterVar.newLine() after the fileWriterVar.write(...) call.

Edited by masijade: typos

0

the alignment is all jumbled up

What are you using to control the alignment?
Embedded spaces or tab characters?

0

managed to solve the problem. by using this

outStream.write(str  +"\n");

Thanks everyone! (:

0

No, use the newLine method of the filewriter as already stated. Different systems use different line endings and you should be using the line ending for the system you're running on (and the newLine method guarantees this) unless you have a specific reason for doing otherwise.

0

alignment is all jumbled up

I guess we didn't understand what "jumbled up" means.
==> Everything on one line.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.