• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Strings in Java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo!

I have a very long String that I want to manipulate in Java. Then row length of this file is 2179 characters.
My program shall replace substrings in that file from a specific start position to an end position.
I have written methods for that but it doesn't work.
After processing my method the row the row is longer than 2179.
For every field I want to replace a string with the row length will be longer!
Why? Is there another why of doing this?

An example of a method is:

 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

You could use string.replace to do the job in one line:

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-

If this is some kind of assignment where you need to do it manually, I am not allowed to just give you a working example here so here I go with hints:

1) constantly re-arranging your StringBuffer like you do is inefficient.

set a start variable to 0

while ( indexOfStringReplace != -1 )
-search for next indexOfStringToReplace in your string starting at start
-append substring(start,indexOfStringToReplace) to your StringBuffer
-append the new string (new value) to your StringBuffer
-set start to indexOfStringToReplace+lenght of the String you were looking for to replace

if (start < originalString.length) //otherwise you had a string to replace at the very end of your string
-Then add the remaining of the string to your string buffer e.g. originalString.substring(start,originalString.length())

2) That's it, you are done. The idea is to iterate in your String to find all occurrence of stringToReplace then, append (build) to your String buffer

3) Don't replace and search in your StringBuffer, just append to it and search in the String instead.

I hope this helps somewhat...


 
Sheriff
Posts: 26776
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like you want to take the "string to replace" and fix it so it's exactly 7 characters long, padding with extra "0" at the beginning if it's too short and truncating if it's too long. Is that right? If so, your way is hard to follow and I think there are easier ways. (If not, then that's because your way is hard to follow )

And then if END_POS - START_POS doesn't refer to 7 characters in the StringBuilder, you're going to change the length of the result by hard-coding "7" in your code. So you shouldn't hard-code the length you expect to replace, you should compute it from the field size.
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do lines 14-15 in your code mean? I suspect there is some confusion there between fields and parameters.
 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Håkan Axheim wrote:


Why not write or even ?
 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also try to come up with more meaningful method name, because at the moment your method name suggests that it should set value. I wouldn't expect to see "return" statement in it.

[edit] and just noticed now, that your method is doing more than 1 task. Trying to set some kind of instance or class variable, as well as returning something.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic