By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,739 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,739 IT Pros & Developers. It's quick & easy.

HELP!!Retrieve product number from SDF file

P: 35
I have a sdf file, i wan to retrieve product numbers from the file. However, when i print out, it only printed out the first product number(02230416222D). There are supposed to be 2 products numbers, does anyone knows why my program only loop 1 time?
This is how the sdf file looks like:
Expand|Select|Wrap|Line Numbers
  1.   -ISIS-  02230416222D
  2.  
  3.  17 18  0  0  0  0  0  0  0  0999 V2000
  4.  
  5. ......
  6. $$$$//this is end of the first product
  7.   -ISIS-  02230416222D
  8.  
  9.  17 18  0  0  0  0  0  0  0  0999 V2000
  10. ......
  11. $$$$ //this is the end of second product
This is my java program:
Expand|Select|Wrap|Line Numbers
  1. Scanner txt = new Scanner(new FileReader("merlin3.sdf"));
  2.  
  3. while(inFile.hasNext()==true)
  4. {            
  5.     String line = inFile.nextLine();
  6.     while(inFile.matches("-ISIS-"))
  7.     {
  8.         String pdt = line.replaceAll("-ISIS-","");
  9.         String pdtNo = pdt.trim();
  10.         System.out.println(pdtNo);
  11.     }
  12. }
Jan 22 '07 #1
Share this Question
Share on Google+
5 Replies


Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. Scanner txt = new Scanner(new FileReader("merlin3.sdf"));
  2.  
  3. while(inFile.hasNext()==true)
  4. {            
  5.     String line = inFile.nextLine();
  6.     while(inFile.matches("-ISIS-"))
  7.     {
  8.         String pdt = line.replaceAll("-ISIS-","");
  9.         String pdtNo = pdt.trim();
  10.         System.out.println(pdtNo);
  11.     }
  12. }
You have declared a Scanner object named txt, but you use an object named inFile in your loop. Should you be using txt instead? The resulting code would be:

Expand|Select|Wrap|Line Numbers
  1. Scanner txt = new Scanner(new FileReader("merlin3.sdf"));
  2.  
  3. while(txt.hasNext()==true)
  4. {            
  5.     String line = txt.nextLine();
  6.     while(line.matches("-ISIS-"))
  7.     {
  8.         String pdt = line.replaceAll("-ISIS-","");
  9.         String pdtNo = pdt.trim();
  10.         System.out.println(pdtNo);
  11.     }
  12. }
Jan 22 '07 #2

P: 35
You have declared a Scanner object named txt, but you use an object named inFile in your loop. Should you be using txt instead? The resulting code would be:

Expand|Select|Wrap|Line Numbers
  1. Scanner txt = new Scanner(new FileReader("merlin3.sdf"));
  2.  
  3. while(txt.hasNext()==true)
  4. {            
  5.     String line = txt.nextLine();
  6.     while(line.matches("-ISIS-"))
  7.     {
  8.         String pdt = line.replaceAll("-ISIS-","");
  9.         String pdtNo = pdt.trim();
  10.         System.out.println(pdtNo);
  11.     }
  12. }
Oh yah, I had paste the wrong one here. Here is my correct code:
Expand|Select|Wrap|Line Numbers
  1. Scanner inFile = new Scanner(new FileReader("merlin3.sdf"));
  2. while(inFile.hasNext("-ISIS-"))
  3. {
  4.     String line = inFile.nextLine();
  5.     String pdt = line.replaceAll("-ISIS-","");
  6.     String pdtNo = pdt.trim();
  7.     System.out.println(pdtNo);
  8. }
Is there any way I can loop the whole file and retrieve the 2 product numbers?
Jan 22 '07 #3

10K+
P: 13,264
Oh yah, I had paste the wrong one here. Here is my correct code:
Expand|Select|Wrap|Line Numbers
  1. Scanner inFile = new Scanner(new FileReader("merlin3.sdf"));
  2. while(inFile.hasNext("-ISIS-"))
  3. {
  4.     String line = inFile.nextLine();
  5.     String pdt = line.replaceAll("-ISIS-","");
  6.     String pdtNo = pdt.trim();
  7.     System.out.println(pdtNo);
  8. }
Is there any way I can loop the whole file and retrieve the 2 product numbers?
Try

Expand|Select|Wrap|Line Numbers
  1. while(inFile.hasNext()) {
  2.     String line = inFile.nextLine();
  3.     if(line.contains("-ISIS-")) {
  4.         String pdt = line.replaceAll("-ISIS-","");
  5.         String pdtNo = pdt.trim();
  6.         System.out.println(pdtNo);
  7.     }
  8. }
Jan 22 '07 #4

P: 35
Try

Expand|Select|Wrap|Line Numbers
  1. while(inFile.hasNext()) {
  2.     String line = inFile.nextLine();
  3.     if(line.contains("-ISIS-")) {
  4.         String pdt = line.replaceAll("-ISIS-","");
  5.         String pdtNo = pdt.trim();
  6.         System.out.println(pdtNo);
  7.     }
  8. }
Thanx for the suggestion, the suggestion works.
Jan 22 '07 #5

10K+
P: 13,264
Thanx for the suggestion, the suggestion works.
Anytime. Of course the contains method is only available in jdk 1.5 and I used it because you were using the scanner.
Jan 22 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.