My journey to Java 002 — Strings, if, for and while

1- Strings:

If you wanna to store a message, then you need a variable with String type. Actually String is a class and when we do String x = "ABC"; is the same as doing String x = new String("ABC"); This gives us the advantage of using the string class methods which are extremly helpfull. Please follow the following example and check the output to recognize the most usefull methods in the string class.

package JournreyToJava002;


public class JourneyToJava002 {

public static void main(String[] args){
String my_first_string = "My Journey to Java, by Islam Taha";
System.out.println(my_first_string.length()); // 33
System.out.println(my_first_string.charAt(0)); // M
System.out.println(my_first_string.indexOf("Islam")); // 23
System.out.println(my_first_string.replace("Islam Taha", "0xIslamTaha")); // My Journey to Java, by 0xIslamTaha

}

}
/*
- str_var.lenght(); returns str_var length
- str_var.chatAt(); returns char at a specific index, not that indexing starts from 0.
- str_var.indexOf("some_thing"); returns the index of "some thing".
- str_var.replace("a", "b"); it replaces all `b` with `a`
*/

2- If condition and control the flow:

To understand the if .. else condition, let we say that we have a login app with “admin:admin” as a username:password credential and you wanna print “Hi! Admin” if the input data is correct and to print “sorry”` if the input data isn’t correct. Here is the code to do that:

String credential = "admin:admin";if (credential=="admin:admin"){
System.out.println("Hi! Admin");
}else{
System.out.println("sorry!");
}

3- For loop:

Hmm, for loop comes to my mind when I want to write the same line of code many times. Let’s stop talking and discuss a problem. Say that we have a string “My Journey To Java By 0xIslamTaha” and I want to print each char of this string in a separate line. we can keep doing

String loop_problem="My Journey To Java By 0xIslamTaha";
System.out.println(loop_problem.charAt(0));
System.out.println(loop_problem.charAt(1));
..
..
System.out.println(loop_problem.charAt(loop_problem.length()-1));
String loop_problem="My Journey To Java By 0xIslamTaha";
for (int i=0; i<loop_problem.length(); i++){
System.out.println(loop_problem.charAt(i));
}

4- While loop:

For me, I dont use while loop :D :D cause I always use for loop but to do the same previous task, here is the snippet.

int x = 0;
while (x<loop_problem.length()){
System.out.println(loop_problem.charAt(x));
x++;
}

5- continue and break statement

lets we combine if statement and the loops. The challenge is, Assume we have a string and we need to check if it has # char ton it or not i.e. we need to search for a specific value and if we find this value, then we can return true and there is no need to continue searching. (we can do this challenge using the string methods. Challenge yourself and do it)

String string_challenge = "0XIslam#Taha!";
for (int i=0; i<string_challenge.length(); i++){
if ('#' == string_challenge.charAt(i)){
System.out.println("Volla!!");
break;
}else{
System.out.println("Keep trying!");
}

--

--

code with love!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store