0

I'm working on an exercise for learning Java where I am supposed to write a method to print to the screen all items that come after the word "category:". This is my attempt at it:

public static void main(String[] args) {
    
    String str = "We have a large inventory of things in our warehouse falling in "
            + "the category:apperal and the slightly "
            + "more in demand category:makeup along with the category:furniture and _.";
    
    printCategories(str);

}

public static void printCategories(String passedString) {
    
    int startOfSubstring = passedString.indexOf(":") + 1;
    int endOfSubstring = passedString.indexOf(" ", startOfSubstring);
    String categories = passedString.substring(startOfSubstring,endOfSubstring);
    
    while(startOfSubstring > 0) {
        System.out.println(categories);
        startOfSubstring = passedString.indexOf((":") + 1, passedString.indexOf(categories));
        System.out.println(startOfSubstring);
        System.out.println(categories);
    }

}

So the program should print:

apperal
makeup
furniture

My attempt is that the program should print the substring where it finds the starting index as ":" and the ending index as " ". Then it does the same thing again, only except from starting the very beginning of the variable str, this time it starts from the beginning of the last category found.

Once there are no more ":" to be found, the indexOf (part of startOfSubstring) will return -1 and the loop will terminate. However, after printing the first category it keeps returning -1 and terminating before finding the next category.

The two lines:

        System.out.println(startOfSubstring);
        System.out.println(categories);

Confirm that it is returning -1 after printing the first category, and the last line confirms that the categories variable is still defined as "apperal". If I comment out the line:

startOfSubstring = passedString.indexOf((":") + 1, passedString.indexOf(categories));

It returns the startOfSubstring as 77. So it is something to do with that line and attempting to change the start of search position in the indexOf method that is causing it to return -1 prematurely, but I cannot figure out why this is happening. I've spent the last few hours trying to figure it out...

Please help :(

2 Answers 2

1

There are a couple of issues with the program:

  1. You're searching passedString for (":") + 1 which is the string ":1", probably not what you want.

  2. You should evaluate endOfSubstring and categories inside the loop.

This is probably close to what you want:

public static void printCategories(String passedString) {
    
    int startOfSubstring = passedString.indexOf(":") + 1;
    
    while(startOfSubstring > 0) {
        int endOfSubstring = passedString.indexOf(" ", startOfSubstring);
        // If "category:whatever" can appear at the end of the string
        // without a space, adjust endOfSubstring here.
        String categories = passedString.substring(startOfSubstring, endOfSubstring);
        // Do something with categories here, maybe print it?
        // Find next ":" starting with end of category string.
        startOfSubstring = passedString.indexOf(":", endOfSubstring) + 1; 
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked!! Thank you very much. I'm going to analyze it in more detail now because I still don't understand exactly what makes it work compared to the old code but I will look it over and figure it out. Thank you!!
I think most of your problem was (":") + 1. You intended to call indexOf with ":" and then add 1 to that, but in fact you appended the string "1" to ":" giving ":1" then passed that to indexOf.
0

I have corrected (in a comment) where you set the new value of startOfSubstring

while(startOfSubstring > 0) { // better if you do startOfSubstring != -1 IMO
    System.out.println(categories);
    // this should be startOfSubstring = passedString.indexOf(":", startOfSubstring +1);
    startOfSubstring = passedString.indexOf((":") + 1, passedString.indexOf(categories)); 
    System.out.println(startOfSubstring);
    System.out.println(categories);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.