cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with Length of String

bhavesh_kantilal
Active Contributor
0 Kudos

All,

I have a for loop that uses the Length of the String as the End Condition.

for(int i=0 ; i<str.length; i++){

char ch=str.charAt(i);

and some more logic..

}

This works fine when the input String is small .

A problem occurs when the Input String becomes too long.

When the input string length has say 1000000 , the problem comes that the loop goes into a never ending loop. I know this has some problem because of the restriction on the size of Integers.

As a work around I thought I would use

for( double i =0; i<str.lenght();i++){

}

but again, charAt expects an Integer as its input and I am stuck as to how to go about this problem.

Any inputs on this are welcome.

Regards

Bhavesh

Accepted Solutions (1)

Accepted Solutions (1)

bhavesh_kantilal
Active Contributor
0 Kudos

All,

Thanks for the ideas.

Actaully, my code was working with the Too Long string also, but it was taking a lot of time ( took around an hour and half to be executed). So, now am trying to refine my code and find out the bottlenecks if any.

All your answers were useful.

Regards

Bhavesh

Former Member
0 Kudos

Bhavesh,

just a doubt why do you have such kind of so long string?

If you could post the complete code maybe we could help you to improve performance, for example if you are creating a string as output it's better to use a <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html">StringBuffer</a> than a simple String for performance reason.

Kind Regards,

Sergio

bhavesh_kantilal
Active Contributor
0 Kudos

Sergio,

Let me give you some background info ( as you know XI you might also be able to help me better).

We are getting a text file from a legacy system and this is contend converted into an XML. So far so good.

The problem is that the file contains Non XML Characters and so, XI's mapping ( Graphical ) is not able to handle the same as it is not a valid XML character , SAX parser throws an exception.

The solution that we arrived at was to use a Module that will search over the input XML by converting the Input XML into a String and then replacing all Non Valid XML characters.

The XML message is a 20 MB XML message and this causes the String to be a huge one.

Regards

Bhavesh

Former Member
0 Kudos

Hi Bhavesh,

In this case I do not think you need to convert the input XML into a big string and work on the string, you can directly work on the inputStream and manipulate it.

I think you should be able to get the inputstream from the object <i>XMLPayload</i> using the method <i>getInputStream()</i>.

Then create a <i>InputStreamReader</i> from the <i>InputStream</i> and A <i>BufferedReader</i> out fo the <i>InputStreamReader</i>, and do something like this:


//create the Buffered Input
BufferedReader bufferInput = new
BufferedReader(new InputStreamReader(in));

//create the output stream buffer
StringBuffer bufferOutput = new StringBuffer();

while ((line = bufferInput.readLine()) != null){
  bufferOutput.append(doYourReplacementwithTheLine(line));
}

YourXmlPayload.setContent(bufferOutput.toString().getBytes());

Anyway, we had once the same requirement, a little bit different because the source message was an Idoc, but we solved the issue using 2 mappings:

- the first (Java Mapping) cleaned the content, directly working on inputStreams from interface <i>StreamTransformation</i>

- the second did the real mapping (graphical mapping)

Maybe you could also evaluate this opportunity!

Hope it helps,

Kind Regards,

Sergio

bhavesh_kantilal
Active Contributor
0 Kudos

Sergio,

Thanks a Bunch for the ideas.

Will try this and and am pretty sure it will work.

Regards

Bhavesh

Answers (3)

Answers (3)

Former Member
0 Kudos

> As a work around I thought I would use

>

> for( double i =0; i<str.lenght();i++){

>

> }

A double is a real number like 1.09088 so it will not work as you have discovered. If you need a whole number that has a greater range than Integer you need to use Long.

Try that and see if it works.

Rudy

Former Member
0 Kudos

I have a feeling it's not related to the restriction on the size of the integers because internally the string is converted to a array of chars. You may want to refactor the "some more logic".

Former Member
0 Kudos

Hi Bhavesh,

it's not elegant but it works and it is easy to code:


int intBufSize = 1000;

String str = "yourlongstring";
String mainString = str;
while(mainString.length()>0){
    str = mainString.substring(0, intBufSize);
    mainString = mainString.replaceFirst(str,""); 
		
    for(int i=0 ; i<str.length(); i++){
        char ch =str.charAt(i);
//        and some more logic..
    }

}

Hope it helps,

Kind regards,

Sergio