Pizzaman
Pizzaman

Reputation: 425

Slicing a string

I'm trying to slice a string for the first time.

With this code, if I input, for example 'one two three' it works fine until the last word. This is the last few lines of the output:

Current word is thr

Sentence is now e

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.substring(String.java:1907)
    at TestCurr.main(testCurrentWord.java:18)

Has anyone any idea why it does that to the last word?

class TestCurr
{
    public static void main (String []args)
    {
        String s;
        int i;
        String currentWord;
        int length;
        int spacePos;

        System.out.println("Enter a sentence ");
        s = EasyIn.getString();
        spacePos = s.indexOf(" ");
        length = s.length();

        for (i = length -1; i >= 0;  i--)
            {
                currentWord = s.substring(0,spacePos);
                s = s.substring(spacePos +1);

                System.out.println("Current word is " + currentWord);
                System.out.println("Sentence is now " + s);
            }
    }
}

Upvotes: 2

Views: 2414

Answers (5)

peter.petrov
peter.petrov

Reputation: 39477

Your current approach is too error prone. And you have too many variables.

Try this just as an idea.

class TestCurr {

    public static void main(String[] args) {
        String s = null;

        System.out.println("Enter a sentence: ");
        s = "   one   two   three   ";
        System.out.println("|" + s + "|");

        int i = 0;
        int j = 0;

        while (true){
            while (i<s.length() && s.charAt(i)==' ') i++;
            j = i;
            if (i>=s.length()) break;
            while (i<s.length() && s.charAt(i)!=' ') i++;
            System.out.println("Current word is: [" + s.substring(j, i)+ "]");
            System.out.println("Sentence is now: [" + s.substring(i) + "]");
            if (i>=s.length()) break;
        }

    }

}

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200296

First of all, you call

spacePos = s.indexOf(" ");
length = s.length();

only once, but these values should change with each iteration of the loop. Furthermore,

s.substring(spacePos +1);

with

spacePos == s.length()-1

means you are passing an index beyond the end of the string as the start index for substring(). Once you fix the first error, this will be your next exception.

Upvotes: 4

Rogue
Rogue

Reputation: 11483

As others have stated, you only get the index once. But I'm curious, why re-invent the wheel?

String s = "one two three";
String[] split = s.split(" ");
for (String out : split) {
    System.out.println("Word: " + out);
}

Upvotes: 0

Jesse Webb
Jesse Webb

Reputation: 45303

I believe your problem is in your usage of your spacePos variable.

Outside the loop, you initialize the variable like so:

spacePos = s.indexOf(" ");

Which in your example string of "one two three", yields 3.

But then inside your loop, you never set the variable again, based on what whatever is left that you haven't processed.

Try re-calculating spacePos's value inside the loop and your problem should go away.

Upvotes: 1

Epiglottal Axolotl
Epiglottal Axolotl

Reputation: 1048

Your problem is that you only get the index of the space once. This causes the program to cut the string every three characters, as the first word is three letters long. You need to update spacePos after each iteration.

Upvotes: 2

Related Questions