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

Islam Taha
3 min readJan 28, 2019

Welcome everybody, You can find code in this GitHub repo. If you don't read the 001, then go and read it now before keep reading this dairy.

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));

but this is a stupid way to program so let's say the smart way :D

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));
}

so here we create a loop starting from i=0 and end when i < the length of the loop_problem.

There is another way to create a loop and we will discuss that when we talk about array.

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++;
}

Hope this diary of the journey is okay for all of you :D :D. The next dairy will be a full implementation of a simple blockchain

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!");
}

to make it simple, Continue means, Hi for loop, skip all coming lines of code inside the loop body and start the next iteration and breakmeans, Hi for loop I’m not any more interesting on you, I’m gonna break the loop without complete it.

Don’t forget to check the code here and keep in touch there is more ….

--

--