• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Read Content Between Pipes Delimiter

 
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Guys I am Writing A code That Reads a text file with fields delimited By a Pipe Character Which I am Able to do. The code Reads the Field Number and The Size. I need To Be able T0 Print the Content of each field Too. Here Is the code I am Using.





Currently Ny output is in the Form:



The Original File is in the Form




I need the output in the form:

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


should be



because '|' is a special character in regex and needs to be escaped if you want to use it literally.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stanley Mungai,

You are almost there

Hint : You are printing length of a String. You also want to print actual String
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thans Jeff For Noticing That I already Have that in the Original Code. That was a Typing error Otherwise the Code would not have Such Output. That Is there In the Original Code.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anayonkar Shivalkar: That is what I do not Know whow to do it. I need Help In That.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:...should be


Or alternatively:
line.split("[|]");
Personally, I hate all those darn backslashes.

Winston
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Winston for that new Idea. I need to Know how to Print out the Content Between The Pipes.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Thank you Winston for that new Idea. I need to Know how to Print out the Content Between The Pipes.



So, you wrote that code yourself, did you?

And you wrote this line?


And you understand how that line works?

And now you're saying you don't know how to append the String itself to that line, even though you've already figured out how to append that String's length? Really? Are you sure about that?

I think either you didn't write the code, or else you're just thinking too hard about it and making it more complicated than it is.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Anayonkar Shivalkar: That is what I do not Know whow to do it. I need Help In That.


You mean to say that you know how to print String's length, but not how to print String itself?
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Guys for helping me figure that Out:



Got it
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool. Glad you figured it out.

Was it a case of just staring at it too long to where your mind got fuzzy and you couldn't see what was staring you in the face? We've all been there.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff: Haha, Guess So, Am so Surprised I could Not see that after Writing So many Codes Like these. I guess I am Tired. I am Going to have some Sleep. Thanks Guys.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a lot to comment, apart from your still using "|" when you should use "\\|". You only managed to get anything because you are probably using the wrong reader. But two errors which cancel each other out do not cease to be errors. Look at the Java Tutorial about regular expressions and the documentation for String#split(java.lang.String) which takes a regiular expression and look at the documentation for FileInputStream which recommends you consider FileReader for text files. Also look in the Java Tutorials about I/O (try where it says Buffered Streams). That should give examples of reading and writing to files.

Why are you creating a new folder just one level from the root of your C drive? Shouldn’t you have that folder in MyDocuments or similar?
Why are you deleting files in line 25? In fact, what do line 22-26 mean at all?
Why are you using a FileInputStream? If you are reading a text file, you are better off using a FileReader.
Why have you got the loop which you never enter in lines 33-35?
Why are you only reading one line from each file? Are you certain they only contain one line each? Shouldn’t you be reading lines in a loop?
Don’t call flush(), least of all before your last writing instruction. You don’t need it. But you most certainly need to close your readers and writers when you finish each file.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ritchie:
1. We are all not Java Geeks Like Probably You are.
2. Yes the File many Lines But am Only interested In the First Line.
3. I am deleting Files Because there were Other Files Named "File" in that Drive Created By other Processes
4. I decided to Create the Folder In Drive C (I Think That Choice is Mine)
5. This is the Reason I should Always remember To Post Only the Relevant Part of the Code
6. This Issue was resolved More than 24 Hours Ago and am Now Only Interested In the beginning Position Of the Field.
7. You removed my Other Link That I was Following Up with the Position.
8. Unless you are Helping Concerning Position, I suggest that the Above Points do not help At all.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Ritchie:
1. We are all not Java Geeks Like Probably You are.

What’s wrong with being a geek?

2. Yes the File many Lines But am Only interested In the First Line.

Thank you for explaining that, which was not clear earlier.

3. I am deleting Files Because there were Other Files Named "File" in that Drive Created By other Processes
4. I decided to Create the Folder In Drive C (I Think That Choice is Mine)

As long as you are using your own computer and don’t mind losing those files. If you are putting code onto somebody else’s computer they might want to keep those files.

5. This is the Reason I should Always remember To Post Only the Relevant Part of the Code
6. This Issue was resolved More than 24 Hours Ago

But there is still a serious issue with flush and close, which remains in the code you posted today

and am Now Only Interested In the beginning Position Of the Field.
7. You removed my Other Link That I was Following Up with the Position.

Sorry, but after your request I restored it.

8. Unless you are Helping Concerning Position, I suggest that the Above Points do not help At all.

One reason for caring about the quality of your code and programming is that I might find myself using it. If anybody is producing code for me to use, I want it to be of the best quality possible, so it will be good for me to use.
Also, many people here are learning to write code to pass exams, The better the quality of the code, the better marks they will receive.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic