JavaRush /Java Blog /Random EN /Coffee break #91. 7 bad programming habits you should bre...

Coffee break #91. 7 bad programming habits you should break right now. indexOf() method in Java

Published in the Random EN group

7 Bad Programming Habits You Should Break Right Now

Source: Hashnode Bad habits are hard to break, especially if you don't realize they're hindering your performance. So, here are seven programming habits that, if you have them, should be abandoned right now. Coffee break #91.  7 bad programming habits you should break right now.  indexOf() method in Java - 1

1. You are not focused on one language

To become a good programmer, you must master the language you work in. If you work in Java, then master Java first, and only then move on to learn another language. Learn step by step. There is no need to learn several programming languages ​​at once.

2. Work as if you have all the answers

Being confident in life is good. But in programming, it's better to constantly check the code you're working on. Even if you are completely confident in yourself, when working on a large project, there will likely be a small syntax error somewhere in your code. To avoid this, simply check your code.

3. Refuse to ask for help or questions.

It is not always possible to solve all problems on your own. A good programmer knows when to ask for help. If you are working on a team project, asking your colleagues for help will not only help you solve the problem, but will also improve your communication with them. This way you can learn more about your team's abilities.

4. Refuse to write bad code

There comes a time in every developer's life when deadlines force them to write terrible code, and that's okay. You've tried to warn your client or manager about the consequences, but they insist on meeting deadlines, so now it's time to write code. Or perhaps there's an urgent bug that can't wait for you to come up with the perfect solution.

5. Don't share what you've learned with your team.

A developer's value lies not only in the code you write, but also in what you learn while working on it. Share your experience, write comments about it, tell others why things are the way they are, and help them learn something new about the project and its intricacies.

6. Blindly copy/paste pieces of code

It's okay to search StackOverflow for answers, but first understand the code you'll be reusing. Sometimes you don't immediately notice everything the code does at first glance. If you spend more time studying the piece of code you copied, you can learn more about the problem that caused you to copy someone else's code.

7. Postponing code correction “for later”

The habit of putting off fixing code “for later” is not just a problem of priorities. Establishing an issue tracking system can lead to some progress, but you also need to be able to track smaller issues. Adding “TODO” to your comments is a quick way to make sure you don't miss anything. There you have it, 7 bad habits you should give up right now. Happy coding!

indexOf() method in Java

Source: Dev.to In this article, I will write about the indexOf() method in the Java programming language. Believe me, this is one of the most useful methods. Coffee break #91.  7 bad programming habits you should break right now.  indexOf() method in Java - 2

Let's start...

The indexOf() method allows you to find a string in another string. It is part of the String class and looks for the first occurrence of a character or substring. This method also returns the index position of the first occurrence of the specified string. In other words, this method retrieves the index value associated with a particular character or substring in a string. If the character or phrase does not appear in the string, indexOf() returns -1. Syntax:
stringName.indexOf(char ch);
There are various options for using indexOf() :

1. indexOf(char ch)

In this variant, we printed the index value of the first occurrence of the character i .
public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of i in this";
        System.out.println("Index of the character i is : " + str.indexOf('i'));
    }
}

2. indexOf(char ch, int start)

In this variant, we printed the index value of character i , but not the first time it appeared. The character i first appears at index 4 , so we assigned it an initial value greater than 4.
public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of i in this";
        System.out.println("Index of the character i is : " + str.indexOf('i', 5));
    }
}

3. indexOf(String str)

In this variant, we printed the index value of the first occurrence of the string in .
public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of in here";
        System.out.println("Index of the String in is : " + str.indexOf("in"));
    }
}

4. indexOf(String str, int start)

In this variation, we printed the index value of String in , but not in its first occurrence, String in first appears at index 9 , so we assigned it an initial value greater than 9.
public class Demo {
    public static void main(String[] args) {
        String str = "We will find index of in here";
        System.out.println("Index of the String in is : " + str.indexOf("in", 10));
    }
}
A common scenario would be when a system administrator wants to find the index of the '@' character in a client's email ID and then wants to get the remaining substring. In this situation, you can use the indexOf method . Thank you for reading.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION