This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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.
Joined: Mar 22, 2005
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.
Joined: Oct 13, 2005
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.