my dog learned polymorphism*
The moose likes Beginning Java and the fly likes RE: reading Text file with the field positions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "RE: reading Text file with the field positions" Watch "RE: reading Text file with the field positions" New topic
Author

RE: reading Text file with the field positions

Chiru Chiranjeevi
Greenhorn

Joined: Nov 18, 2011
Posts: 3
I wanna read a text file from 1-10(positions in the text ) to a variable..I tried with the below code but I got an String BufferoutIndexException .help me on how to get the output.
Thanks in advance..


BufferedReader reader = new BufferedReader(new FileReader("C:/cf.txt"));
String line;
while ((line=reader.readLine()) != null )
{
System.out.println(line);
contact.setPid(line.substring(11,17).trim());
contact.setBirthDt(line.substring(18,43).trim());
contact.setEmailAddr(line.substring(44,143).trim());
//getting the fields to diefferenclass getback.data() method

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please copy/paste the exact, complete error message--with the full stack trace, and make it clear which line is causing it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Do you really have a line with at least 143 characters?? Because that's what your code is doing - read the first line (until the first line break), then split that up.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Also please paste in the actual exception name. There is no such thing as "BufferoutIndexException" in the core API.
Chiru Chiranjeevi
Greenhorn

Joined: Nov 18, 2011
Posts: 3

output:

DB2 Database Connected
4824157
43319
4824158
43318
4824159
43317

cf file :
first line:
YesIamtestingtheworkingofthefunctionwhichmustprintsomeresultstomeanditisnotgivingtheresultssowhattodoihavetogetitbytodayhopesomeo
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

In the future, please UseCodeTags, so that your code will be readable.

The error message is telling you exactly what's wrong, and exactly where. At line 98 of SaveBean.java you're trying to access an index beyond the last index of the String in question. As rob suggested, you're probably assuming there are more characters present than there actually are.

Add println() statements at each step so you can see what each piece is, and also, before all that, print out the length of the line.
Chiru Chiranjeevi
Greenhorn

Joined: Nov 18, 2011
Posts: 3
Thanks for the quick response..Now this site becomes my homepage
 
 
subject: RE: reading Text file with the field positions