asdascascaedfa
asdascascaedfa

Reputation: 137

is not equal to the symbols(java)

Sorry for my English. I want to find out for yourself one point, but I did not get. I want that line of output used in the case of the word. But I have prints like this: if I want to find a word - one in line is the application - ones entered the convent home. It is still output the line. And in this situation do not want displayed.

public static void main(String[] args) throws FileNotFoundException {
    String line = "line number ones";
    String word = "num";
    String myWord = "";
    int startWord = 0;


    for(int i = 0; i < line.length()-1; i++) {
        if(line.charAt(i) == word.charAt(startWord)) {
            startWord++;
            myWord += line.charAt(i);

            if(myWord.equals(word)) {
                if(startWord == word.length()) {
                    System.out.println(line);
                }
                startWord = 0;
            }
        }
    }
}

As need to make additional checks. something like:

if(Character.toString(line.charAt(i+1)) != any characters except space) {
output line
}

Turn out like this:

if(startWord == word.length() && Character.toString(line.charAt(i+1)) == " ") {
                        System.out.println(line);
                    }

But it does not work. And we must also take into account that can be a point, a comma, just the end of the line. How to do it?

Upvotes: 0

Views: 90

Answers (1)

Rob
Rob

Reputation: 315

Are you trying to check if a character is equal to space?If so, you cannot use == on a String because it is an object. But you can use it on a char.

So two ways are like this:

String space = " ";
if (space.equals (" ")) // true

Or

char space = ' ';
if (space == ' ') // true

In your example, you would do this:

Character.toString(line.charAt(i+1)).equals (" ")

Upvotes: 1

Related Questions