• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

"StringIndexOutOfBoundsException" while appending data to a file.

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having trouble while appending the data to the same file, though tried but getting the "StringIndexOutOfBoundsException".
How should I resolve, please suggest.

Here is my code



Here is the error. My output file is pointing to the dataoutput file and it still does not get it to append.

 
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java Seeker wrote:

The problem is with a call to substring(0,10) for a string that is empty. You have not given us the code for URLShortener, line 101, where the error occurred.
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps your data.txt file contains an empty line. Even if it does then your code should have the logic to deal with that instead of throwing an exception.
 
Arya Ramanujan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ok, here is the Url Shortener code :

 
Arya Ramanujan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey - removing the empty lines, the error is gone. However, the data does not append to dataoutput.txt file at all !
I am not understanding where my code is going wrong, it is pretty much generic method of bookish implementation.
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You did not include the 'import' lines in your code so your line numbers don't match your exception stack trace. Your error is on this line 2. You are trying to get the first 10 characters of 'url'. What if 'url' has less than that?

A better way to do this is with the beginsWith() String method.
You have a similar issue with line 8. What if the length of the String is zero?
A better way would be to use the endsWith() String method.
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java Seeker wrote:

On line 7 you create 'bw' pointing to your file 'dataoutput.txt'. On line 19 you create 'fw' pointing to the same file *while the file is still opened from line 7*. Why do you need 'pw' at all, just use your 'bw'.

Also, newer versions of MS Windows don't allow you to have data files at the root directory, they need to be inside a folder.
Correction: Apparently it is perhaps allowed but discouraged.
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:On line 7 you create 'bw' pointing to your file 'dataoutput.txt'. On line 19 you create 'fw' pointing to the same file *while the file is still opened from line 7*. Why do you need 'pw' at all, just use your 'bw'.

You should probably use PrintWriter so that you can use the println() method, but you shouldn't be opening up the file each time you get a line from data.txt. So, replace line 7 with the PrintWriter code you have on line 19 and then do away with the inner try block except for the call to pw.println().
 
Arya Ramanujan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you have suggested, I have changed it the following way. Still does not append. Any thoughts ?

 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still have both pw and fw. You only need one. You should only open your output file once, not inside the loop.
 
Arya Ramanujan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, the implementation worked. I was thinking wrong in outputting the data...I think Object Oriented a bit tough when compared to traditional scripting.
But will practice more to get acquainted with Java. Now, I will try to fix the startWith() and endWith() for the URLShortener method.
 
Carey Brown
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You constructor creates this, essentially a constant, for each new instance. It would be better implemented as an actual constant so that there is only one of them.
In your generateKey() method you are concatenating chars onto a String. This results in a new String object being created for each iteration of the loop. Using a StringBuilder avoids this inefficiency.
Note that your current implementation does work and that my suggestion might be deemed "premature optimization", but often when you see a better way to do something it ends up becoming second nature.
 
Arya Ramanujan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Carey. This worked nicely and one of the better implementations. I have used it in couple of occasions and worked charm. Appreciate your help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic