• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

string extraction by giving position as first argument in command prompt

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ,

when the user enters the position as args[0] in the command prompt . the program should search for the string at this position and write string found at that position to the file . here is the code . but it is not returning any string



could anyone help me out please
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the complete code snippet? Looks like you might some compile errors if you try to run this.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be useful.
 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are nowhere telling the Reader to read the line.
You would probably be better off using the Scanner class than a Reader.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several problems jump out at me...

1) your while statement on line 7 has 2 "("
2) you are getting the substring of variable line on line 8 which has never had a value assigned to it since you declared it.
3) you are declaring a BufferedReader to read a file but then your variable bf is never used.
4) on line 4 you ask the user to enter a position to start extracting from.... but the user has already entered thier choice at this point and so there is no point in asking. Plus there is no way for the user to enter anything after this statement is printed. This will end up in user confusion/anger.

I hope that helps you rethink your approach.
 
pavithra murthy
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all ,

from the string "MRN 09DE588004180289E0" i have to extract only the 18 digit number after MRN . this is actually present in a output.doc file . I am currently not able to extract that from the file even after using file writer .
when i run no output is coming and output should actually be 09DE588004180289E0 on the command prompt
could anyone please help me out

that 18 is fine but here the problem is i have to read the value fom a text output.doc file such that if i enter search string only MRN it should output to me the "09DE588004180289E0" on the command prompt . i have used filereader but still dont know the reason why it is not showing the ouput .


1. //package com.test.pdf;
2.
3. //package jPDFTextSamples;
4.
5. import java.io.FileWriter;
6. import java.io.File;
7. import java.io.FileInputStream;
8. import java.io.IOException;
9. import java.io.InputStream;
10. import java.io.Reader;
11. import java.io.StringReader;
12. import java.util.Date;
13. import java.lang.String; //import org.pdfbox.util.TextPosition;
14. import java.io.*;
15. import java.lang.*;
16.
17. import java.io.FileNotFoundException;
18. import java.nio.ByteBuffer;
19. import java.nio.channels.FileChannel;
20. import java.util.regex.*;
21. import java.nio.*;
22. import java.nio.channels.*;
23. import java.nio.charset.*;
24.
25. //import com.qoppa.pdfText.PDFText;
26.
27. public class ExtractAllText2 {
28. public static void main(String[] args) {
29. try {
30. // Load the document
31. // PDFText pdfText = new PDFText ("Exportbegleitdokument.pdf",
32. // null);
33.
34. // Get the text for the document
35. // String docText = pdfText.getText();
36.
37. /*
38. * // Save the text in a file FileWriter output = new FileWriter
39. * ("output.doc"); output.write(docText); output.close();
40. *
41. * //want user to enter the string to extract File file = new
42. * File("output.doc");
43. */
44. // ....................................................
45. BufferedReader bf = new BufferedReader(new FileReader("d:\\output.doc"));
46. int linecount = 0;
47.
48. String line="";
49. boolean isWordMatched = false;
50.
51.
52.
53. String value = "";
54. String search = "MRN";
55.
56. while ((line = bf.readLine()) != null) {
57.
58. if (line.indexOf(search) != -1) {
59.
60. int valStartIndex = line.indexOf(search) + search.length()+ 1;
61.
62. int valEndIndex = line.indexOf(search) + search.length()+ 1 + 18;
63.
64. value = line.substring(valStartIndex, valEndIndex);
65.
66. isWordMatched = true;
67.
68. System.out.println("value>>>>:" + value);
69.
70. }
71.
72. }
73.
74.
75. } catch (Throwable t) {
76. t.printStackTrace();
77. }
78. }
79. }
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the CODE button; your code is illegible and un-compilable with those line numbers.

And do you really need import java.lang.*; ?
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!