Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes reading from file an expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading from file an expression" Watch "reading from file an expression" New topic

reading from file an expression

Haythem Ben Yahia

Joined: Nov 29, 2012
Posts: 14
i have a file "*.txt" that contains a lot of informations seperated by ";"
i wanted just to read the second and the third argument.can you please help me doing that?
this is my code that allows me to read text file

how can i read only the 2nd and the third argument from this file??

thank you
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172

If your information is separated by ";"
Read line
Split (hint) the string using the ";" delimiter
Process required information

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Krishna Kanth
Ranch Hand

Joined: Oct 23, 2006
Posts: 49

You can use StringTokenizer and then read 2nd, 3rd items from resultant array
or simply use sub string, index of methods from String class to read 2md, 3rd fields

P.S: kind of repeated what Maneesh said :-)
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7552

krishna kanth wrote:You can use StringTokenizer...

You can, but I'd suggest you don't. StringTokenizer is a legacy class, which has been superceded by Scanner (java.util.Scanner).

However, you don't even need that. You already have what looks like perfectly good code for processing lines, so just do what Maneesh suggested for each line (even bigger hint than he gave: look at String.split() ).


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
I agree. Here's the link:
subject: reading from file an expression
Similar Threads
how to split in java and remove certain lines from that text?
Random Percentage of Calls to Commands
Trying to get manageable arrays to work with
adding String Array in .conf(text also) file after particular line by java?
Read from a file with three lines but the length is 116