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

1- Strings:

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:

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

3- For loop:

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:

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

5- continue and break statement

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