Newbie question: Why is the substring comparison not happening in line 30 ?

olcal28olcal28 India
edited January 2016 in Java

I have just started learning Java online. I have to search and print out multiple substrings which are part of a larger string. In the problem given I have to locate "classification:clocks", "classification:propellers", "classification:tools" but have to printout clocks, propellers, tools. I do not understand what I am doing wrong as there are no syntax errors. Why is the substring comparison not happening in line 12

public class LoopsPractice

    public static void main(String[] args)

        String str = "We have a large range of goods in our storage falling in "
                + "the classification:clocks and "
                + "classification:propellers along with classification:tools and others"; // This is the input string


// Expected output is clocks
//                    propellers
//                    tools

    public static void printClassification(String str)
        int endIndex = str.length();
        int currentIndex = 0;
        int spaceIndex = 0;

        while (currentIndex <= endIndex) // setup loop index range 
            if((currentIndex+9) < endIndex) // test to prevent search for "classification:" from going beyond string length
                System.out.println(str.substring(currentIndex, (currentIndex+9)));
                if (str.substring(currentIndex, (currentIndex+15)) == "classification:") // **Comparison not happening, why ?**
                    spaceIndex = currentIndex; // setup index to count characters until space character

                    while (str.substring(spaceIndex, (spaceIndex+1))!= " ")
                        spaceIndex = spaceIndex + 1;
                    System.out.println(str.substring(currentIndex, spaceIndex)); 
        currentIndex = currentIndex + 1;
        System.out.println("All done");


  • There is a mistake in my question. It should read "Why is the substring comparison not happening in line 30"

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion