like taht ,please freinds any body give idea to split the file according to my requirement.
thanks in advance
bye Naga
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Welcome to the Ranch.
Probably easier to use a Scanner, which automatically splits using a regular expression. Put the name of the file into the constructor of the Scanner, and leave out the 2nd argument. Then the Scanner will split on its default delimiter (=whitespace). Use the hasNext() method as the test for a while loop. Use the next() method to read the next token inside the while loop
From a BufferedReader you will get a String object for each line of text. You can then use the String.split method to cut that line into pieces according to your requirements.
this is my code .part is print the gap beteween characters . what my question is how to divide first five characters in one string,next 8 characters in another sting,next 7 characters in another sting. i don't have any idea abt that so please give the code for that.actually that is wrong asking but no idea in my mind so plase help me.
bye naga
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Scanner uses delimiter characters. If that's not how you want to divide up the string, use the BufferedReader method you had before (and which I commented on). That will give you each line of the file as a string. Then you can use the String methods (like substring) to carve out the pieces you're interested in.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
I think your problem is that you are using the wrong delimiter. I said to use the default delimiter, so you oughtn't to call the useDelimiter() method. You are also reading the first word from each line, so you will have to read several times in the while loop. Try again with those changes and I think you will get what you want.
Alternative: When your Scanner (or your BufferedReader) reads a line, it is a String. Use the split() method of String (again without specifying a delimiter) and you get the line divided into an array of Strings.
Susanta Mandal
Greenhorn
Joined: Feb 25, 2008
Posts: 1
posted
0
Hi naga raaju Try the following code(after modifying your code), i think it will work as your requirement,if not then modify some more like this using String or StringBuffer class methods.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Welcome to the Ranch, Susanta Mandal, but please don't simply give out answers like that. Read this.
Your answer is actually not very efficient; you don't need two Scanners.