I need your urgent help. Please help me to solve it out.

Thanks a lot in advanced for your time.

error:

java.lang.StringIndexOutOfBoundsException: String index out of range: 150

Lines of code in this place there:

             if (text.length ()> 150) {
                 text.replaceAll summary = ("<[^>] *>", ""). substring (0, 150) + "...";
             }
             else
                 text.replaceAll summary = ("<[^>] *>", "");

How do one change this code for the error disappears!

Edited 4 Years Ago by pritaeas: Not a code snippet.

Looks like you should use 149.....

Remember positions start at 0 and end at one less of your declared value.

Example:

int[] hi=new int[5];

Has these positions:

hi[0]
hi[1]
hi[2]
hi[3]
hi[4]

@riahc3

You mean to say like this.

 if (text.length ()> 150) {
                 text.replaceAll summary = ("<[^>] *>", ""). substring (0, 149) + "...";
             }
             else
                 text.replaceAll summary = ("<[^>] *>", "");

Let me know if i understand well or not. Please correct me.

What happens when you compile and execute the code?

There are several extra spaces in the posted code. Is what you posted copied from your code or did you type the post in from memory?

Edited 4 Years Ago by NormR1

@NormR1

I would like to know the correction in the above code. If you know, try to help for correct it. Can one use stringWidth ??

@NormR1

It gives error wheni execute the code

java.lang.StringIndexOutOfBoundsException: String index out of range: 150

Yes, you already said that in your first post.
The code you have posted in not valid Java. It won't compile, and therefore cannot be executed. So how about posting the actual code that gave you the error?

Edited 4 Years Ago by JamesCherrill

Yes, you already said that in your first post.
The code you have posted in not valid Java. It won't compile, and therefore cannot be executed. So how about posting the actual code that gave you the error?

I could have sworn I commented that....

I gave you what I THINK it could be......if you dont post the entire code, we have no idea.

you have written a wrong java code.
please go through the correct syntax of String.replae() method.
Then use it.
First go and learn some basic concept of replace() method and String class.

Thanks

@jalpesh 007

Thanks for your time. May be i am not good programmer, but i am sure that there is some problem within this code only. I always think twice before putting code. I have tried to correct the code. It can quite possible, we can write the code in better way.

if(text.length() > 150){
                summary = text.replaceAll( ("<[^>]*>", "  ").substring(0, 150)" , "...");
            }
            else
                summary = text.replaceAll(("<[^>]*>", "  ") , " " );

Thank you ALL.

summary = text.replaceAll( ("<[^>]*>", " ").substring(0, 150)" , "...");

Your code syntax is still wrong. Also, even if it is correct, your logic could fail again. The reason is that you modify the String before you take its substring. If you are going to modify a string, do it before you check for its length. I will give you a scenario of what is going to happen.

String text = "012345555556789";
String anotherText="";
if (text.length()>11) { anotherText = text.replaceAll("7[^8]*8", "").substring(0, 11); }
else { anotherText = text.replaceAll("7[^8]*8", ""); }

What the code portion above does is similar to yours. It replaces anything between number 7 and 8 from the string before it takes the substring of the text. The code above works and would not have a problem because the result text string is "0123455555569" whose length is still longer than 11.

Now replace the string with the below code portion.

String text = "012345677777789";
String anotherText="";
if (text.length()>11) { anotherText = text.replaceAll("7[^8]*8", "").substring(0, 11); }
else { anotherText = text.replaceAll("7[^8]*8", ""); }

This time, the program is still the same -- replace anything between number 7 and 8. However, the result string will be "01234569" which is much shorter than 11! This will give you the StringIndexOutOfBounds!!!

When you program, you must always remind yourself what are happening to variables in your code. If you ever have had a modification to your variable, make sure that the modification does NOT affect the next step you are going to do. The reason is that the variable is now changed from one state to the other. You must ensure that the state of the variable is still in the scope you are working on. In your current case, the state of the variable could affect what you are going to do next with it.

Edited 4 Years Ago by Taywin

@Taywin

Thousand thanks for the example and explanation. I am very happy to have the proper WAY of do the program. I really appriciate the way you explain me my mistake and how i can correct it.

I have modify it once again.

       var summary = text.replaceAll ("<[^>] *>", "") 

        if (summary. length ()> 150) 
        {
               summary = summary. substring (0, 150) + "...";
        }
        else
        {
               summary = text.replaceAll("<[^>]*>", "");
        }

Now, is it good? Let me know if you have other idea.

Do you have to replace the string regardless the length of it? If so, you do not need the else statement because it is redundant.

PS: The regex of "<[^>] *>" and "<[^>]*>" are not the same. The former has a white space; where as, the latter does not.

Edited 4 Years Ago by Taywin

Ok.

var summary = text.replaceAll("<[^>]*>", "");  

            if(summary .length() > 150){       
                   summary = summary .substring(0, 150)+"...";
                }
            }

You can save time by trying your code in the Java compiler first - for example it would have immediately told you that "var" isn't Java (I think you meant String), rather than waiting 20 minutes for someone to read your post and reply. ;)

This question has already been answered. Start a new discussion instead.